有人能让我了解sortedArrayUsingFunction 的工作原理吗?

发布于 2024-12-13 21:43:24 字数 918 浏览 2 评论 0原文

我的数组具有以下内容:

{
   "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"]
   }

我想按创建的(秒值)排序,

这就是我所拥有的......我只是不知道sortedArrayUsingFunction是如何工作的。我的意思是我在比较函数中比较什么?

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜唯美灬不弃 2024-12-20 21:43:24

num1num2 是数组的 2 个元素,上下文是一个对象,您可以将其传递到函数中以帮助您进行排序。

您的函数将在数组上多次调用,排序结果将在新数组中返回给您。

这就是你想知道的吗?

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?

北方的巷 2024-12-20 21:43:24

调用比较函数来一次比较 NSArray 中的两个值。这就是排序的完成方式。

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

千寻… 2024-12-20 21:43:24

比较函数是告诉排序算法您希望如何对对象进行排序的方式。没有它,它就无法知道最终的顺序应该是什么。

在您的示例代码中,参数名称 num1num2 非常具有误导性。将它们更准确地命名为 object1object2。如果object1应该出现在object2之前,则返回NSOrderedAscending。如果它应该在NSOrderedDescending之后,否则返回NSOrderedSame

为了说明这一点,下面是一个按年龄(从最低到最高)对假设的 Person 对象进行排序的示例。

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;
    }
}

请注意,我什至没有使用上下文参数,因为我的对象本身有足够的信息来确定顺序。

如果我希望它们按高度降序排序,那么我可以传递以下内容:

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;
    }
}

非常重要的一件事是,如果参数以其他顺序传递,您的函数应该返回一致的结果。例如,如果 tallest_first(adam, joe, NULL) 返回 NSOrderedDescending,则 tallest_first(joe, adam, NULL) 应返回 NSOrderedAscending< /代码>。如果不是,你的比较函数就会自相矛盾。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文