开发者

iphone - sorting an array of dictionaries

开发者 https://www.devze.com 2023-01-04 01:14 出处:网络
My app has 3 kinds of objects: A, B and C. Each object is represented by a dictionary like this: NSDictionary *oneObject = [NSDictionary dictionaryWithObjectsAndKeys:

My app has 3 kinds of objects: A, B and C. Each object is represented by a dictionary like this:

NSDictionary *oneObject = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:i], @"number",
    [NSString stringWithSt开发者_如何学编程ring:@"A", @"kind", //this can be A, B or C
    [NSNumber numberWithBool:YES], @"used", // YES or NO
    [NSString stringWithString:oneName, @"name",
    nil];

Objects like that is stored on a array like this:

NSMutableArray *listOfObjects = [NSMutableArray arrayWithObjects:
     oneObject1, oneObject2 ..etc.. , nil];

So, I have a mutable array of dictionaries.

Now I want to sort this mutable array, obtaining a new array in the following order

1) all objects A
2) all objects B
3) all objects C

all objects are sorted in their categories... something like

object A, apple
object A, banana
object A, orange
object B, car
object B, donkey
object B, phone
object B, zebra
object C, ant
object C, box
object C, cigar
object C, hotel
object C, lamb

I mean, each category is sorted by the object name but the whole array is sorted by the category kind.

How can I do that? thanks for any help.


using the same method here you can modify your compare function to do what you need:

How to sort an NSMutableArray with custom objects in it?

To have a separate array being returned you'll most likely have to create a copy of your current array then have that one sorted. You'll then have two, your original and your copy that's sorted.

0

精彩评论

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