Strong Like Bull
Strong Like Bull

Reputation: 11317

Can someone make me understand how sortedArrayUsingFunction works?

my array has the following:

{
   "4eb57e72c7e24c014f000000" : {
     "_id" : {
       "$id" : "4eb57e72c7e24c014f000000"
     },
     "author" : "tim",
     "comments" : [],
     "created": {
       "sec" : 1320517234,
       "used" : 856000
     },
     "picture" : "http://someurl.com",
     "text" : "this is a test",
     "title" : "test",
     "type" : ["test"]
   }

I want to sort by created (sec value)

this is what I have....I just do not know how sortedArrayUsingFunction works. I mean what am I comparing in the compare function??

jokesArray = [unSortedContentArray sortedArrayUsingFunction:Sort_Created_Comparer context:self]; 

NSInteger Sort_Created_Comparer(id num1, id num2, void *context)
{
    int v1 = [num1 getSecFromJSONValue];
    int v2 = [num2 getSecFromJSONValue];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

Upvotes: 0

Views: 403

Answers (3)

jjwchoy
jjwchoy

Reputation: 1908

The comparison function is your way of telling the sort algorithm how you want your objects ordered. Without it, it has no way of knowing what the final order should be.

In your example code the argument names num1 and num2 are very misleading. They would be more accurately named object1 and object2. If object1 should come before object2 then return NSOrderedAscending. If it should come after then NSOrderedDescending otherwise return NSOrderedSame.

To illustrate, here is an example that sorts hypothetical Person objects by age (lowest to highest).

NSInteger youngest_first(id object1, id object2, void *context) {
    if (object1.age < object2.age) {
        return NSOrderedAscending;
    }
    else if (object1.age > object2.age) {
        return NSOrderedDescending;
    }
    else {
        return NSOrderedSame;
    }
}

Notice that I didn't even use the context parameter as my objects themselves had sufficient information to determine the order.

If I instead want them to be sorted by descending height then I could pass the following:

NSInteger tallest_first(id object1, id object2, void *context) {
    if (object1.height > object2.height) {
        return NSOrderedAscending;
    }
    else if (object1.height < object2.height) {
        return NSOrderedDescending;
    }
    else {
        return NSOrderedSame;
    }
}

One thing that is very important is that your function should return a consistent result if the arguments are passed in the other order. For example if tallest_first(adam, joe, NULL) returns NSOrderedDescending then tallest_first(joe, adam, NULL) should return NSOrderedAscending. If not your comparison function contradicts itself.

Upvotes: 0

Vincent Bernier
Vincent Bernier

Reputation: 8664

num1 and num2 are 2 elements of your array and context is an object that you can pass in to your function to help you with the sort.

Your function will be called many times on the array and the result of the sort will be returned to you in a new array.

Is that what you are wondering?

Upvotes: 1

Michael Frederick
Michael Frederick

Reputation: 16714

The comparison function is called to compare two values at a time from your NSArray. This is how the sorting is done.

Upvotes: 0

Related Questions