1

How can I perfrom a custom sorting operation on an NSArray. I have one array of strings which is my ordering that I want.

NSArray A = {cat, dog, mouse, pig, donkey}

And I have one array of strings which is not ordered the way I want.

NSArray B = {dog,cat,mouse,donkey,pig}

Whats the best way to put array B in the same order as array A without having to use keys?

dubbeat
  • 7,706
  • 18
  • 70
  • 122
  • try to add elements in same order to the `NSArray *B;` when you init it or use the `-initWithArray:` method to init the new array, or without any keys you can use any brutal force algorithm to reorder the new array's elements as you wish because it seems the order of the element are undetermined in your example. – holex Aug 03 '12 at 10:16
  • brutal force is probably the way. I was hoping there was some NSOrdering Kung fu I didnt know about – dubbeat Aug 03 '12 at 10:18
  • if you have any deterministic rule for the sorting the elements you would use anyone of the `-sortedArrayWith...' methods but in lack of it you cannot. because **the key is always needed** to sort them, just many of cases the values can be used as keys. – holex Aug 03 '12 at 10:23

4 Answers4

7

Here's a way

NSArray *sortedArray = [B sortedArrayUsingComparator: ^(id obj1, id obj2){
    NSUInteger index1 = [A indexOfObject: obj1];
    NSUInteger index2 = [A indexOfObject: obj2];
    NSComparisonResult ret = NSOrderedSame;
    if (index1 < index2)
    {
        ret = NSOrderedAscending;
    }
    else if (index1 > index2)
    {
        ret = NSOrderedDescending;
    }
    return ret;
}];

The above will sort the elements of B into the same order as A with elements that are in B but not in A appearing at the end (since NSNotFound is a very big number). The only problem with the algorithm is that it multiplies the algorithmic complexity of the sort by O(n) where n is the number of objects in A. So for large A it will be pretty slow.

jpm
  • 16,622
  • 34
  • 63
  • 66
JeremyP
  • 84,577
  • 15
  • 123
  • 161
3

If you have:-

NSArray A = {cat, dog, mouse, pig, donkey}

and

NSMutableArray B = {dog,cat,mouse,donkey,pig}

you can use:-

[B sortArrayUsingComparator:^NSComparisonResult(NSString *obj1,   NSString *obj2) {
    NSUInteger indexOfObj1 = [A indexOfObject: obj1];
    NSUInteger indexOfObj2 = [A indexOfObject: obj2];
    if(indexOfObj1 == NSNotFound || indexOfObj2 == NSNotFound){
        return NSOrderedSame;
    }
    else if(indexOfObj1 > indexOfObj2){
        return NSOrderedDescending;
    }

    return NSOrderedAscending;
}];
Yogesh Maheshwari
  • 1,324
  • 2
  • 16
  • 37
0

Check out sortedArrayUsingComparator, always works for me!

Example:

NSArray *sortedArray = [B sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1,   NSString *obj2) {
    //Insert custom ordering code here, this will just sort alphabetically.
    return [obj1 compare:obj2];
}];
JDx
  • 2,615
  • 3
  • 22
  • 33
0

to add custom sorting the best way is to use sotring with function

NSArray *B=[A sortedArrayUsingFunction:sortingFunction context:nil];
//  sortingFunction

NSInteger sortingFunction( id obj1, id obj2, void *context){ if ( //your condition ){ return NSOrderedDescending; } if ( //your condition){ return NSOrderedAscending; } return NSOrderedSame; }

touti
  • 1,164
  • 6
  • 18