我有一个类具有不同的属性,如地名,地点距离,用户名,。我正在将此类对象存储在数组中。我想根据距离对此对象数组进行排序。我可以这样做。请指示一些代码。
答案 0 :(得分:1)
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"placeDistance" ascending:TRUE];
[your array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]
答案 1 :(得分:0)
NSArray* array...
NSArray* sorted_array = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
YourClass* a = (YourClass*)obj1;
YourClass* b = (YourClass*)obj2;
/*
return NSComparisonResult based on a property comparison here,
for example, return [a.distance compare:b.distance] if distance is an NSNumber
*/
}];