简体   繁体   English

对NSArray自定义对象应用排序

[英]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.

相关问题 如何根据一个属性对对象的 NSArray 进行排序 - How to sort NSArray of objects based on one attribute 如何按字母顺序对带有字典对象的 NSArray 进行排序? - How to sort NSArray with dictionary objects alphabetically? 如何按2列/键对对象的NSArray进行排序 - How to sort an NSArray of objects by 2 column/key 如何根据NSArray中自定义对象的日期进行排序...? - How to sort based on date of custom object in NSArray…? NSArray中的NSUserDefaults和自定义对象的条件编码 - NSUserDefaults and Conditional Encoding of Custom Objects in an NSArray 在NSUserDefaults中存储我的自定义对象的NSArray - Storing a NSArray of my Custom Objects in NSUserDefaults 如何使用另一个包含对象的有序键的NSArray对对象的NSArray进行排序? - How to sort an NSArray of objects using another NSArray which contains the ordered keys of objects? 使用自定义对象对数组进行排序 - Sort array with custom objects 从自定义对象的NSArray中删除重复项,并对其进行比较和编辑 - Remove duplicates from NSArray of Custom Objects also compare and edit same 将自定义对象的NSArray转换为带有索引的按字母顺序划分的UITableView? - Turning an NSArray of custom objects into a alphabetically sectioned UITableView with an index?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM