0

i have array of NSDictionary like below

array(
        {
        Birthdate = "03/20";
        Names = "Abhinavswati Chaudhary";
        remain = 0;
    },
        {
        Birthdate = "09/22/1986";
        Names = "Aisa Soni";
        remain = 186;
    },
        {
        Birthdate = "03/02/1990";
        Names = "Anuj Parekh";
        remain = 347;
    },
        {
        Birthdate = "05/22/1988";
        Names = "Ajay Desai";
        remain = 63;
    }
)

how can i sort this array in ascending with accordance to remain for example it should be like

 array(
       {
       birthdate=...
       name =abhivan
       remanin =0;
   },
      {
       brithdate = "05/22/1988";
       name =ajay
       remain =63
   },
      {
       and so on....

plz suggest some code thank you in advance

when we use NSsortDiscripor then remain need to be NSNumber, can it be the reason that my remain numbers are actually string ?

user2189388
  • 202
  • 1
  • 4
  • 10

1 Answers1

2

This is quite simple by using the sortedArrayUsingComparator method of NSArray.
In your case it should look like:

yourArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    // get values
    NSNumber *remain1 = obj1[@"remain"];
    NSNumber *remain2 = obj2[@"remain"];

    return [remain1 compare:remain2];
}];
yinkou
  • 5,756
  • 2
  • 24
  • 40