简体   繁体   中英

Sorting NSMutableArray with class objects for a peticular object in alphabetical order

I have an array with property class objects like

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

}

I want to sort this array for eventData.eventsname key in alphabetical order. I tried out with following functionality, but it is not working. Please suggest me how to do this. Thanks in advance

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
 NSArray *orderedArray = [self.arrEventDataList sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

I want to sort my arrEventDataList sorted by eventsname .

Try this.

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

You will have the sorted values. This will sort your arrEventDataList .

Try This One...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM