【问题标题】:Sorting Array Based on custom object values根据自定义对象值对数组进行排序
【发布时间】:2013-03-25 08:23:59
【问题描述】:

我需要Sort 出array 的自定义对象。

object 很简单,它存储了一个 x 和 y 坐标,例如:

XYPointObject.xCoordinate = [NSNumber];
XYPointObject.yCoordinate = [NSNumber];

我有一个array,它最多可以存储 676 个这些对象,我需要将这些对象sort 按数字顺序存储在 x 值中,y 值也按顺序连接到 x 值。例如,如果输入坐标是:

23,5 | 
5,7 | 
1,4 | 
1,7 | 
21,8 | 
9,12 | 
16,19

排序后的数组将有顺序

1,4 | 1,7 | 5,7 | 9,12 | 16,19 | 23,5 

请记住最大 x,y 坐标为 25 (25,25)

【问题讨论】:

    标签: objective-c nsarray nssortdescriptor


    【解决方案1】:

    你有没有看过documentation?

    这是您需要的方法:

    -[NSArray sortedArrayUsingComparator:]
    

    【讨论】:

      【解决方案2】:

      用途:

      NSSortDescriptor *xSorter = [[NSSortDescriptor alloc] initWithKey:@"xCoordinate" ascending:YES];
      NSSortDescriptor *ySorter = [[NSSortDescriptor alloc] initWithKey:@"yCoordinate" ascending:YES];
      NSArray *sortedArray=[array sortedArrayUsingDescriptors:@[xSorter,ySorter]];
      

      【讨论】:

        猜你喜欢
        • 2013-11-25
        • 2021-05-02
        • 2017-09-29
        • 2021-04-26
        • 1970-01-01
        • 2018-11-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-12
        相关资源
        最近更新 更多