根据自定义对象值对数组进行排序 [英] Sorting Array Based on custom object values
本文介绍了根据自定义对象值对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要排序
自定义对象的array
.
object
很简单,它存储了一个 x 和 y 坐标,例如:
The object
is simple, it stores an x and y co-ordinate, for example:
XYPointObject.xCoordinate = [NSNumber];
XYPointObject.yCoordinate = [NSNumber];
我有一个 array
可以存储多达 676 个这些对象,我需要将这些对象按数字顺序 sort
排序,如 x 值按数字顺序,以及 y 值也按顺序连接到 x 值.例如,如果输入坐标是:
I have an array
that stores up to 676 of these objects that I need to sort
into a numerical order as in x values in numerical order, and the y values connected to the x values in order as well. for example, if the input co-ordinates are:
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)
keep in mind the max x,y co-ordinants are 25 (25,25)
推荐答案
使用:
NSSortDescriptor *xSorter = [[NSSortDescriptor alloc] initWithKey:@"xCoordinate" ascending:YES];
NSSortDescriptor *ySorter = [[NSSortDescriptor alloc] initWithKey:@"yCoordinate" ascending:YES];
NSArray *sortedArray=[array sortedArrayUsingDescriptors:@[xSorter,ySorter]];
这篇关于根据自定义对象值对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文