Thursday 6 December 2012

How can I get an array of the indexes from another array that has been sorted?

You can also use below of the code, May its useful to you,
    NSSortDescriptor *_lastDescriptor = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];
NSArray *_lastArray = [NSArray arrayWithObject:_lastDescriptor];


firstCharacterArray = (NSMutableArray *)[[nameIndexesDictionary allKeys] sortedArrayUsingDescriptors:_lastArray];
//firstCharacterArray = (NSMutableArray *)[[nameIndexesDictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
for (NSString *eachlastIndex in firstCharacterArray)
{
    NSSortDescriptor *lastDescriptor = [[NSSortDescriptor alloc] initWithKey:@""
                                                                   ascending:YES];
    //selector:@selector(localizedCaseInsensitiveCompare:)] ;
    NSArray *descriptorslast = [NSArray arrayWithObject:lastDescriptor];
    [[nameIndexesDictionary objectForKey:eachlastIndex] sortUsingDescriptors:descriptorslast];
    [lastDescriptor release];
}



feedback
Here's what I ended up doing:
//Create a mutable array of the indexes in the myArray (just a list from 0...n)

NSMutableArray *indexes = [[NSMutableArray alloc] init];

for (int i = 0; i < myArray.count; i++){
    [indexes addObject: [NSNumber numberWithInteger:i]];
}

//Create a dictionary with myArray as the objects and the indexes as the keys
NSDictionary *tempDictionary = [NSDictionary dictionaryWithObjects:myArray forKeys:indexes];

//Create an array of myArray's keys, in the order they would be in if they were sorted by the values 
NSArray *sorted = [tempDictionary keysSortedByValueUsingSelector: @selector(compare:)];

No comments:

Post a Comment