views:

59

answers:

3

I want to sort an array by its objets and then get the indexes like so:

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects: @"3", @"2", @"1", @"0", @"1", @"2", nil];

I want the indexes of the objects in ascending order. Here, because the lowest value has an index of 3, the indexes would be would be: 3, 2, 4, 1, 5, 0 or something like that.

Any ideas?

Thanks!

+1  A: 

just sort it and use indexOfObject:. Like so:

NSArray *sorted = [myArray sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *indices = [NSMutableArray array];
for (id object in myArray)
  [indices addObject: [NSNumber numberWithInteger: [sorted indexOfObject: object]]];

(Out of my head, hope this works.)

Max Seelemann
Great! This almost works. The only problem is that the because there are multiple objects with the same index, it is only returning the lowest index value. (See in the example above.) Do you know how I can get it to return all index values?
Jonah
@Jonah: You have a fundamental issue with getting more than one index in this case because the two objects @"1" and @"1" in your example are logically equal in that `-isEqual:` returns YES and are almost certainly actually the same object (because the compiler strips out duplicate constant strings).
JeremyP
The two strings ARE the very same object. You won't be able to distinguish them. What you might do is to replace all objects in myArray with a placeholder like `NSNull`. The loop will gat a little more complicated then, though...
Max Seelemann
+1  A: 

hello,

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];
}
milanjansari
A: 

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:)];
Jonah