为什么我的排序结果返回完全随机?

发布于 2024-12-14 02:05:05 字数 1032 浏览 2 评论 0原文

我的数组笑话数组具有如下元素。我试图按嵌套元素“sec”排序。

每次运行此命令时,我都会得到不同的排序顺序,或者每次返回相同的视图时(我的排序代码位于 viewwillappear 中)。为什么?

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

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

    NSInteger Sort_Created_Comparer(id array1, id array2, void *context)
{

    int v1 = (int)[[array1 objectForKey:@"created"] objectForKey:@"sec"];
    int v2 = (int)[[array2 objectForKey:@"created"] objectForKey:@"sec"];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

my array jokesarray has elements like the following. I am trying to sort by nested element "sec".

Everytime I run this I get a different sort order or every time I come back to the same view (my sorting code is in viewwillappear). Why?

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

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

    NSInteger Sort_Created_Comparer(id array1, id array2, void *context)
{

    int v1 = (int)[[array1 objectForKey:@"created"] objectForKey:@"sec"];
    int v2 = (int)[[array2 objectForKey:@"created"] objectForKey:@"sec"];
    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

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

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

发布评论

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

评论(2

蘸点软妹酱 2024-12-21 02:05:05
[[array1 objectForKey:@"created"] objectForKey:@"sec"]

返回一个指向对象的指针;在您的情况下,是一个 NSNumber 对象。当您将其转换为 int 时,如下所示:

(int)[[array1 objectForKey:@"created"] objectForKey:@"sec"]

您将该对象所在的内存地址转换为 int 值,因为 -objectForKey:返回一个指向对象的指针。

由于您对内存地址不感兴趣,而是对底层 int 值感兴趣,因此请使用 -[NSNumber intValue]

int v1 = [[[array1 objectForKey:@"created"] objectForKey:@"sec"] intValue];

此外,您将字典命名为 -[NSNumber intValue] 也很奇怪code>array1 和 array2 — 它们不是数组。


编辑: 或者,您可以让 NSNumber 为您进行比较:

NSInteger Sort_Created_Comparer(NSDictionary *d1, NSDictionary *d2, void *context)
{
    NSNumber *n1 = [[d1 objectForKey:@"created"] objectForKey:@"sec"];
    NSNumber *n2 = [[d2 objectForKey:@"created"] objectForKey:@"sec"];
    return [n1 compare:n2];
}
[[array1 objectForKey:@"created"] objectForKey:@"sec"]

returns a pointer to an object; in your case, an NSNumber object. When you cast it to int like:

(int)[[array1 objectForKey:@"created"] objectForKey:@"sec"]

you’re casting the memory address where that object is located to an int value, since -objectForKey: returns a pointer to an object.

As you’re not interested in the memory address but the underlying int value instead, use -[NSNumber intValue]:

int v1 = [[[array1 objectForKey:@"created"] objectForKey:@"sec"] intValue];

Also, it’s odd that you’ve named your dictionaries array1 and array2 — they aren’t arrays.


Edit: Alternatively, you could let NSNumber do the comparison for you:

NSInteger Sort_Created_Comparer(NSDictionary *d1, NSDictionary *d2, void *context)
{
    NSNumber *n1 = [[d1 objectForKey:@"created"] objectForKey:@"sec"];
    NSNumber *n2 = [[d2 objectForKey:@"created"] objectForKey:@"sec"];
    return [n1 compare:n2];
}
喜爱皱眉﹌ 2024-12-21 02:05:05

方法 -objectForKey: 返回一个 NSNumber 对象指针。它不能直接转换为数字,调用[NSNumber intValue]来获取该对象的值。

但是,您可能希望做得更简单:

jokesArray = [unSortedContentArray sortedArrayUsingDescriptors:
              [NSArray arrayWithObject:
               [[NSSortDescriptor initWithKey:@"created.sec"
                                    ascending:YES] autorelease]]];

Method -objectForKey: returns a NSNumber object pointer. It cannot be directly cast to number, call [NSNumber intValue] to get the value of this object.

However, probably you may want to do that simpler:

jokesArray = [unSortedContentArray sortedArrayUsingDescriptors:
              [NSArray arrayWithObject:
               [[NSSortDescriptor initWithKey:@"created.sec"
                                    ascending:YES] autorelease]]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文