2

I have a NSMutableArray called allItems which has the following ProductData object.

Each object has cid, cname, ctype and cimage. As you see below json object is not coming in order. However, cid is a indicator for ordering.

I wonder how do you order based on cid?

 [
  {
    "cid": "2",
    "cname": "Meats",
    "ctype": "main",
    "cimage": "baked_chicken.jpg"
  },
  {
    "cid": "1",
    "cname": "Dips",
    "ctype": "side",
    "cimage": "stuffed_eggplant.jpg"
  },
  {
    "cid": "4",
    "cname": "Sandwiches",
    "ctype": "sand",
    "cimage": "chickenshawarma.jpg"
  },
  {
    "cid": "3",
    "cname": "Appetizers",
    "ctype": "side",
    "cimage": "rice_lentils.jpg"
  },
  {
    "cid": "5",
    "cname": "Desserts",
    "ctype": "dsrt",
    "cimage": "cake.jpg"
  }]

If I use the sortDescriptior, it partially works. The issue that I am facing, when it sorts, it first display cid 1 and then cid 10 and then cid 2.

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES];
[allItems sortUsingDescriptors:@[sortDescriptor]];
casillas
  • 16,351
  • 19
  • 115
  • 215

4 Answers4

6

Use a sort descriptor with a comparator passing compare:options: and option NSNumericSearch:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES comparator:^(id obj1, id obj2) {
     return [obj1 compare:obj2 options:NSNumericSearch];
}];

or even simpler:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES selector:@selector(localizedStandardCompare:)];

localizedStandardCompare is a special comparison selector:

This method should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact sorting behavior of this method is different under different locales and may be changed in future releases. This method uses the current locale.

vadian
  • 274,689
  • 30
  • 353
  • 361
1

Use this

NSSortDescriptor *aSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES comparator:^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];
sortedArray = [NSMutableArray arrayWithArray:[unsortedArray sortedArrayUsingDescriptors:@[aSortDescriptor]]];
1
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"cId" 
    ascending:YES selector:@selector(caseInsensitiveCompare:)];

arrToBeSort = [arrToBeSort sortedArrayUsingDescriptors:[NSArray 
    descriptor]];

this will work

Vinodh
  • 5,262
  • 4
  • 38
  • 68
Nimit
  • 145
  • 1
  • 8
0
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid.intValue" ascending:YES];
[array sortUsingDescriptors:@[sortDescriptor]];

Your "array" has now been sorted after print it

Printing description of array: <__NSArrayM 0x618000041bf0>( { cid = 1; cimage = "stuffed_eggplant.jpg"; cname = Dips; ctype = side; }, { cid = 2; cimage = "baked_chicken.jpg"; cname = Meats; ctype = main; }, { cid = 3; cimage = "rice_lentils.jpg"; cname = Appetizers; ctype = side; }, { cid = 4; cimage = "chickenshawarma.jpg"; cname = Sandwiches; ctype = sand; }, { cid = 5; cimage = "cake.jpg"; cname = Desserts; ctype = dsrt; } )

Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35