0

我有一个带有属性类对象的数组,例如

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]];

我想对按 eventsname 排序的 arrEventDataList 进行排序。

4

2 回答 2

1

尝试这个。

NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"eventsname"
                                                                 ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
NSArray *sortedArray = [arrEventDataList sortedArrayUsingDescriptors:sortDescriptors];

您将获得排序后的值。这将对您的arrEventDataList.

于 2013-06-11T10:52:59.163 回答
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]];
于 2013-06-11T11:18:40.203 回答