我有一个包含属性类对象的数组,如
for (int i=0; i<[eventList count]; i++) {
NSDictionary *EventListDic=[eventList objectAtIndex:i];
eventData.eventID=[EventListDic objectForKey:@"event_id"];
eventData.eventsname=[EventListDic objectForKey:@"title"];
[self.arrEventDataList addObject:eventData];
[eventData release];
}
我想按字母顺序为eventData.eventsname
键排序此数组。我尝试了以下功能,但它无法正常工作。请建议我如何做到这一点。提前致谢
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
NSArray *orderedArray = [self.arrEventDataList sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
我想按eventname排序我的arrEventDataList。
答案 0 :(得分:1)
试试这个。
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"eventsname"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
NSArray *sortedArray = [arrEventDataList sortedArrayUsingDescriptors:sortDescriptors];
您将拥有已排序的值。这会对您的arrEventDataList
进行排序。
答案 1 :(得分:0)
试一试......
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
NSArray *sortedFloats = [eventData.eventsname sortedArrayUsingDescriptors:
[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
// Put the two arrays into a dictionary as keys and values
NSDictionary *dictID = [NSDictionary dictionaryWithObjects:eventData.eventID forKeys:eventData.eventsname];
// Sort the second array based on the sorted first array
sortedID = [dictID objectsForKeys:sortedFloats notFoundMarker:[NSNull null]];