[英]Applying a sort to NSArray of custom objects
I have an array of custom objects (really an NSSet stored in Core Data which becomes an NSArray with AllObjects) and each of these custom objects is a ListItem
. 我有一个自定义对象数组(实际上是存储在Core Data中的NSSet,它成为带有AllObjects的NSArray),每个自定义对象都是一个
ListItem
。 Each ListItem
belongs to a List
, which has a uniqueID. 每个
ListItem
属于一个List
,它具有唯一ID。 Each ListItem
has a different uniqueID which is the uniqueID of the List
it belongs to + a number. 每个
ListItem
都有一个不同的uniqueID,它是它所属的List
的唯一ID +一个数字。 So if the id of my List
object is @"foo", then its ListItems
would each have the property ListItem1.id = @"foo0", ListItem2.id = @"foo1", ListItem3.id = "foo2" etc.. 因此,如果我的
List
对象的id是@“foo”,那么它的ListItems
将各自具有属性ListItem1.id = @“foo0”,ListItem2.id = @“foo1”,ListItem3.id =“foo2”等。
So, how can I sort my array of custom objects based on the number that is appended to this uniqueID on each ListItem? 那么,我如何根据每个ListItem上附加到此uniqueID的数字对自定义对象数组进行排序?
NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(ListItem *obj1, ListItem *obj2){
return [obj1.id compare:obj2.id];
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.