sorting a custom class
Possible Duplicate:
How to sort an NSMutableArray with custom objects in it?
I have a custom class where one of the values are names. Imagine the class is called Person. I would like to sort these values, which I am keeping in an NSMutableArray according to the name variable, however:
[testarray sortUsingSelector:@selector(compare:)];
will only work only an array of strings.
I am trying to work out how I can arrange the array alphabetically given that:
Person *person;
person = [array objectAtIndex:0];
[person name];
Person *person;
person = [array objectAtIndex:1];
[person name];
How can I compare the two? Thanks!
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];
链接地址: http://www.djcxy.com/p/70820.html
上一篇: 如何在iPhone中以降序排列数组
下一篇: 排序自定义类