1

I have an iOS app with a NSMutableArray which contains many rows worth of data (split up into 3 segments) - as shown below:

[PFUser object, stringData, floatValue]
       .             .          4.3
       .             .          5.9
       .             .          1.1
       .             .          9.32
       .             .          0.024

The above diagram shows how the array is split up and the data types that are stored in the array. But those data types are not keys or anything. So I can't do something like valueForKey.

An example of what the array looks like:

@[@[userObject, @"hello", @234.1441],
  @[userObject, @"sdfsg", @94.33],
  @[userObject, @"wrfdo", @24.897]
];

So from the example above you can see that I have arrays in arrays.

I would like to sort the array by reading the 3 segment which contains the float values.

How can I do this? I have looked online and read a lot about using NSSortDescriptor but the problem I have is that all the examples always seem to use simple strings or an array with numbers only.

Is there a way to use NSSortDescriptor in an array with custom objects like mine?

3
  • I am certain that cannot be done in a clean way. Because you will violate the sorting invariants all the time. create a custom class to store the connected data. Commented Jan 7, 2016 at 21:33
  • 2
    To be clear, you have an array of arrays where each array has 3 values, correct? The example you posted is missing commas after each inner array. Commented Jan 7, 2016 at 21:35
  • @rmaddy Yes that is correct. Commented Jan 7, 2016 at 21:35

3 Answers 3

4

You can use sortUsingComparator:

[array sortUsingComparator:^(id obj1, id obj2) {
    NSArray *arr1 = obj1;
    NSArray *arr2 = obj2;
    return [arr1[2] compare:arr2[2]];
}];

Or even (thanks to rmaddy's suggestion):

[array sortUsingComparator:^(NSArray *arr1, NSArray *arr2) {
    return [arr1[2] compare:arr2[2]];
}];

If you have a immutable array, you can use sortedArrayUsingComparator:

Sign up to request clarification or add additional context in comments.

3 Comments

This can be made even easier by changing the type of obj1 and obj2 to NSArray * from id. Then the body can be one line.
For questions "How do I sort..." the correct answer is always either "use sortUsingComparator and write the code" or "no, you can't sort dictionaries" :-)
@gnasher729 true :))
3

Try this :

For example in your case to sort the array by float value :

NSMutableArray *array = // Init your array;

array = [NSMutableArray arrayWithArray:[array sortedArrayUsingComparator:^(id obj1, id obj2) {
    float value1 = [[obj1 objectAtIndex:2] floatValue];
    float value2 = [[obj2 objectAtIndex:2] floatValue];

    if (value1 < value2) {
        return (NSComparisonResult)NSOrderedDescending;
    } else if (value1 > value2) {
        return (NSComparisonResult)NSOrderedAscending;
    } else {
        return (NSComparisonResult)NSOrderedSame;
    }
}]];

With this method you can sort as you like.

6 Comments

1) Why create the mutable array? 2) Why bother with the float values? Just get each NSNumber from index 2 and compare those two objects directly. No need for the if/else.
See the answer by Cristik for what I meant. It's must simpler.
sortUsingComparitor: is definitely the thing, but why not just return [obj1[2] compare:obj2[2]]?
I create a mutable array because sortedArrayUsingComparator return an array. It is an example with float the code must be adjust. I added the if / else to compare any object with any object, for example sort by string and float.
"the code must be adjust" - nonsense. You have NSNumber. NSNumber knows perfectly fine how to compare with another NSNumber.
|
0

NSMutableArray has a call sortUsingComparator, which takes a comparison result block as parameter. So Something like this:

[users sortUsingComparator:
     ^NSComparisonResult(id obj1, id obj2)
     {
         PFUser* userA = (PFUser*)obj1;
         PFUser* userB = (PFUser*)obj2;

        return [userA.floatValue compare:userB.floatValue];

     }];

2 Comments

This won't work. It's not an array of PFUser objects.
Who ever upvoted this doesn't understand the question. This answer won't work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.