开发者

How to sort NSMutable array in ascending order

How to sort NSMutable array in ascendin开发者_运维问答g order. Can any one help me out this.


You haven't said what's in your array, but if it's something that responds to -compare: then you can use

[myArray sortUsingSelector:@selector(compare):];


Here is a way to sort an array of object

    NSSortDescriptor * descFirstname = [[NSSortDescriptor alloc] initWithKey:@"firstname" ascending:YES];
NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"lastname" ascending:YES];
[myArrayOfPerson sortUsingDescriptors:[NSArray arrayWithObjects:descLastname,descFirstname, nil]];
    [descFirstname release];
    [descLastname release];


Yes, you can use a NSSortDescriptor, have a look at Sort Descriptor Programming Topics.


NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:nil
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [sortValuesArray sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"sortedArray%@",sortedArray);

sortValuesArray is the array that you want to sort.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜