1

I want to insert an NSDictionary object into an array and have the array sorted by objectForKey.

I managed to do this by inserting, re-sorting and reloading the array, which works fine, but I figured there would be a better method, which there is, I just don't know how to use it.

Current code:

-(NSMutableArray*)sortArray:(NSMutableArray*)theArray {
    return [[theArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"n"
                                                                                                           ascending:YES]]] mutableCopy];
}

returns the re-sorted array. Fine. Works perfectly, but... is it the most efficient?

From another question I gather that this is the method to use to find the index to insert into:

NSUInteger newIndex = [array indexOfObject:newObject
                         inSortedRange:(NSRange){0, [array count]}
                               options:NSBinarySearchingInsertionIndex
                       usingComparator:comparator];

[array insertObject:newObject atIndex:newIndex];

The problem is I have no idea how to use the comparator method, and if that's even possible when I want to sort by objectForKey for each index.

Can anyone exemplify the above method when the key of the object (newObject in above example) to sort by is, let's say:

[newObject objectForKey:@"sortByThis"];
nickdnk
  • 4,010
  • 4
  • 24
  • 43

3 Answers3

0

here is the simple example for comparator

- (NSComparisonResult)compareResulst:(CustomObject *)otherObject {
   if(self.name isEqualToString:key)
   {
      return NSOrderedAscending;
   }
   else
   {
       return NSOrderedDescending
   }
}

NSArray *sArray;
sArray = [unsArray sortedArrayUsingSelector:@selector(compareResulst:)];

this will sort the array. it will iterate through object and based on your if else it will move object up and down ...

Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • Could you elaborate on this? Perhaps with a complete example. I don't understand how any of the above could possible get me the correct index to insert an object into? – nickdnk Sep 15 '14 at 09:24
  • use dictionary's key to and compare self and other object. and return NSComparisonResult ... let me try to modify the example – Mihir Mehta Sep 15 '14 at 09:31
  • Doesn't this just return my sorted array - as what I'm already doing? I'm trying to fetch which indexPath to insert into to prevent re-sorting the entire array. – nickdnk Sep 15 '14 at 10:09
  • Well then it's no better than my current method. I am still rebuilding the entire array this way. I wanted to get the indexPath and insert the object into the array that way, for efficiency. I guess in my situation though, it doesn't really matter. – nickdnk Sep 15 '14 at 12:03
0

Use NSSortDescriptor

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortByThisKey" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
fdlr
  • 101
  • 1
  • 11
  • This re-sorts the entire array, it doesn't give the ability to insert at proper index, which is what I'm trying to accomplish. – nickdnk Sep 15 '14 at 09:22
0

Here's an example of sorting

//For make a we're adding 5 random integers into array inform of NSDictionary
//here, we're taking "someKey" to store the integer (converted to string object) into dictionary
NSMutableArray *array = [NSMutableArray array];
for(int i = 1; i<=5; i++) {
    [array addObject:[NSDictionary dictionaryWithObject:[@(arc4random()%10) stringValue] forKey:@"someKey"]];
}

//create a sort descriptor to sort the array
//give the key in dictionary for which you want to perform sort 
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"someKey" ascending:YES];
//we're doing self sort for mutable array, & that's it, you'll have a sorted array.
[array sortUsingDescriptors:@[sort]];

N.B.

1.Instead of "someKey" from above example you can set any key for which you want to perform sort.

2.There's some other methods for sorting, you can use the one base on your requirement.

3.see @[sort] in code. Its full version is, [NSArray arrayWithObject:sort];

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • This is what I'm already doing. I updated my question with current code. I was looking for a way to avoid using the SortDescriptor and instead fetch the indexPath which would match the sorting of the array. – nickdnk Sep 15 '14 at 10:05
  • 1
    Ok, you can check for the function possible execution time, [check this](http://stackoverflow.com/questions/2129794/how-to-log-a-methods-execution-time-exactly-in-milliseconds). – Hemang Sep 15 '14 at 10:09
  • Thanks, I'll have a look at that. – nickdnk Sep 15 '14 at 10:11