简体   繁体   中英

Sort NSMutableArray based on strings from another NSArray

I have an NSArray of strings that I want to use as my sort order:

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 *order = [NSMutableArray arrayWithArray:[permissions allKeys]];

How can I always sort my order array correctly based on my using the permissionTypes array as a key?

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM