how to sort an NSArray using compare:options

后端 未结 2 496
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 05:44

I have an NSArray containing numbers as NSString objects. ie.

[array addObject:[NSString stringWithFormat:@\"%d\", 100]];

How do I sort th

2条回答
  •  再見小時候
    2020-12-18 05:54

    Since your objects are numbers, instead of using NSString objects, you could use NSNumber objects (easily turned into strings via the stringValue property) and sort the array using sortedArrayUsingDescriptors:.

    For example:

    NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
    [sorter release];
    NSArray *sortedArray = [anArray sortedArrayUsingDescriptors:sorters];
    [sorters release];
    

提交回复
热议问题