0

i have three array like below

names                      birthdate                remanning 

"Abhi Shah",                 "01/14",                  300
"Akash Parikh",              "12/09/1989",             264
"Anand Kapadiya",            "12/01",                  256
"Annabella Faith Perez",     "03/02",                  347
"Aysu Can",                  "04/14/1992",             25
"Chirag Pandya"              "10/07/1987"              201

plz it will be great help if you give me code for how to add this three array in NSDictionary and then after ordering(ascending) whole dictionary in accordance to "remaning" array

Note in Dic everything should get changed. not only remaning array. names and birthdate should get changed in same way remaning days are getting changed

thank you so much in advance

2
  • u want to sorta ccording to remaining days? Commented Mar 20, 2013 at 7:19
  • 1
    you add same question in stackoverflow.com/questions/15517015/… Commented Mar 20, 2013 at 7:31

3 Answers 3

3

I would advise you to change the design of your project and create a model for same as having properties :

@interface YourModel : NSObject
    @property (strong) NSString *name;
    @property (strong) NDDate *birthDate;
    @property NSInteger remaining;
@end

And then create an NSMutableArray in your class, and go on to add them.

This will make your work easier, as searching, sorting, filtering , than handling 3 parallel arrays.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to take each record (names, birthdate, remanning ) in the dictionary or any structure. And array of that dictionary should be created. To sort the array as per your requirement any sorting mechanism can be used.

-(void)sort
{
    //This is the array of dictionaries, where each dictionary holds a record
    NSMutableArray * array; 
    //allocate the memory to the mutable array and add the records to the arrat

    // I have used simple bubble sort you can use any other algorithm that suites you
    //bubble sort
    //
    for(int i = 0; i < [array count]; i++)
    {
        for(int j = i+1; j < [array count]; j++)
        {
            NSDictionary *recordOne = [array objectAtIndex:i];
            NSDictionary *recordTwo = [array objectAtIndex:j];

            if([[recordOne valueForKey:@"remaining"] integerValue] > [[recordTwo valueForKey:@"remaining"] integerValue])
            {
                [array xchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }   
    }

    //Here you get the sorted array
}

Hope this helps.

Comments

1

If you use the design proposed by Anoop, the sorting code using blocks would be something similar to this:

NSArray *sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSInteger first = [(YourModel*)a remaining];
    NSInteger second = [(YourModel*)b remaining];
    return [first compare:second];
}];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.