[英]Sort NSMutableArray based on strings from another NSArray
I have an NSArray
of strings that I want to use as my sort order:我有一个
NSArray
字符串,我想用作我的排序顺序:
NSArray *permissionTypes = [NSArray arrayWithObjects:@"Read", @"Write", @"Admin", nil];
I then have a NSMutableArray
that may or may not have all three of those permissions types, but sometimes it will only be 2, sometimes 1, but I still want it sorted based on my permissionsTypes
array.然后我有一个
NSMutableArray
,它可能有也可能没有所有这三种权限类型,但有时它只会是 2,有时是 1,但我仍然希望它根据我的permissionsTypes
数组进行排序。
NSMutableArray *order = [NSMutableArray arrayWithArray:[permissions allKeys]];
How can I always sort my order
array correctly based on my using the permissionTypes
array as a key?如何始终根据我使用
permissionTypes
数组作为键对我的order
数组进行正确排序?
I would go about this by creating a struct or an object to hold the permission types.我会通过创建一个结构或对象来保存权限类型来解决这个问题。
Then you can have...那么你可以拥有...
PermissionType
--------------
Name: Read
Order: 1
PermissionType
--------------
Name: Write
Order: 2
and so on.等等。
Then you only need the actual array of these objects and you can sort by the order value.然后你只需要这些对象的实际数组,你可以按顺序值排序。
[array sortUsingComparator:^NSComparisonResult(PermissionType *obj1, PermissionType *obj2) {
return [obj1.order compare:obj2.order];
}];
This will order the array by the order field.这将按顺序字段对数组进行排序。
NSMutableArray *sortDescriptors = [NSMutableArray array];
for (NSString *type in permissionTypes) {
NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:type ascending:YES] autorelease];
[sortDescriptors addObject:descriptor];
}
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
Use whichever sorting method on NSMutableArray
you prefer, you will either provide a block or a selector to use for comparing two elements.在
NSMutableArray
您喜欢的任何排序方法,您将提供一个块或一个选择器来用于比较两个元素。 In that block/selector rather than comparing the two strings passed in directly look each up in your permissionTypes
array using indexOfObject:
and compare the resulting index values returned.在那个块/选择器中,而不是比较传入的两个字符串,使用
indexOfObject:
在你的permissionTypes
数组中直接查找每个字符串,并比较返回的结果索引值。
I suggest you another approuch:我建议你另一种方法:
- (void)viewDidLoad
{
[super viewDidLoad];
arrayPermissions = [[NSMutableArray alloc] init];
NSDictionary *dicRead = [NSDictionary dictionaryWithObjectsAndKeys:
@"Read", @"Permission", nil];
NSDictionary *dicWrite = [NSDictionary dictionaryWithObjectsAndKeys:
@"Write", @"Permission", nil];
NSDictionary *dicAdmin = [NSDictionary dictionaryWithObjectsAndKeys:
@"Admin", @"Permission", nil];
NSLog(@"my dicRead = %@", dicRead);
NSLog(@"my dicWrite = %@", dicWrite);
NSLog(@"my dicAdmin = %@", dicAdmin);
[arrayPermissions addObject:dicRead];
[arrayPermissions addObject:dicWrite];
[arrayPermissions addObject:dicAdmin];
NSLog(@"arrayPermissions is: %@", arrayPermissions);
// create a temporary Dict again
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys: arrayPermissions, @"Permission", nil];
// declare one dictionary in header class for global use and called "filteredDict"
self.filteredDict = temp;
self.sortedKeys =[[self.filteredDict allKeys]
sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"sortedKeys is: %i", sortedKeys.count);
NSLog(@"sortedKeys is: %@", sortedKeys);
}
hope help希望帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.