0

I have three arrays. They are name, birthdates and remaining days like below:

    name                         birthdate                 remaining 

    "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

I want to rearrange the remaining days array into ascending order, but at the same time name and birthdate should also get reordered in the same way.

See this example below:

    name                         birthdate                 remaining 

    "Aysu Can",                  "04/14/1992",             25
    "Chirag Pandya"              "10/07/1987"              201
    "etc..."
1
  • 2
    It's called Object-oriented programming for a reason ;-) Create an object that holds those 3 variables and get rid of your multi array reordering mess. Commented Mar 20, 2013 at 7:01

5 Answers 5

3

use NSMutableDictionary to store data to NSMutableArray

NSMutableArray *array=[[NSMutableArray alloc]init];
 for (int i=0; i<totalRows; i++)
 {
        NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
        [dic setValue:[array_1 objectAtIndex:i] forKey:@"names"];
        [dic setValue:[array_2 objectAtIndex:i] forKey:@"birthdate "];
        [dic setValue:[array_3 objectAtIndex:i] forKey:@"remanning"];
        [array addObject:dic];
        [dic release];
 }

here after you arrange name array,use search option to use name not use index and use NSPredicate search data in NSMutableArray

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"names matches[cd] %@", name];
 NSArray *result = [array filteredArrayUsingPredicate:predicate];
 NSMutableDictionary *dict = [[[result objectAtIndex:0] mutableCopy] autorelease];
 NSLog(@"%@",dict);// result
Sign up to request clarification or add additional context in comments.

3 Comments

i dont understand this predicate will you explain how it going to sort?
it not sort array it get particular dic in array,you sort only name array,using sorted name array and use predicate to get particular name details in array
[NSPredicate predicateWithFormat:@"names matches[cd] %@", name]; here names is a key and name is searching user name
2
    self.arrayForRows = [[NSMutableArray alloc]init];
    NSMutableArray *arrayForNames = [[NSMutableArray alloc]initWithObjects:@"Abhi Shah",@"Akash",@"Nagavendra",@"Ramana",@"Simhachalam", nil];
    NSMutableArray *arrayForBirthDates = [[NSMutableArray alloc]initWithObjects:@"01/14/94",@"01/14",@"11/07/87",@"12/07/89",@"23/08/91", nil];
    NSMutableArray *arrayForRemaining = [[NSMutableArray alloc]initWithObjects:@"200",@"320",@"32",@"450",@"14", nil];


    for (int i=0; i<arrayForBirthDates.count; i++)
    {
       NSMutableDictionary *tempDicts = [[NSMutableDictionary alloc]init];
       [tempDicts setObject:[arrayForNames objectAtIndex:i] forKey:@"names"];
       [tempDicts setObject:[arrayForBirthDates objectAtIndex:i] forKey:@"birth"];
       [tempDicts setObject:[NSNumber numberWithInt:[[arrayForRemaining objectAtIndex:i] intValue]] forKey:@"remaining"];
       [self.arrayForRows addObject:tempDicts];
    }  


    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"remaining" ascending:YES];
    [self.arrayForRows sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

Use this in tableView listing

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arrayForRows count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
    }
    cell.textLabel.text = [[self.arrayForRows objectAtIndex:indexPath.row] valueForKey:@"names"];
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

9 Comments

dic get created but it not getting sort with NSSortDEscriptor whats wrong?
It is working for me fine. how you are accessing in your cellForRowAtIndexpath method ? could you tell me ?, see my edited answer
CellForRowAtIndexPath is later thing when i NSLOG arrayForRows after ur code then still it remain the way it was, array of dic do not get ordered in ascending order
I am getting it yar perfectly. You just copy and paste my code and tried ??
Got your point.. Now check i had updated the code.. It will work fine
|
2

once try like this it'l help you,

 NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict setObject:@"rahul" forKey:@"name"];
    [dict setObject:@"10" forKey:@"value"];
    NSMutableDictionary *dict1=[[NSMutableDictionary alloc]init];
    [dict1 setObject:@"ttt" forKey:@"name"];
    [dict1 setObject:@"6" forKey:@"value"];

    NSMutableArray *ar=[[NSMutableArray alloc]init];
    [ar addObject:dict];
    [ar addObject:dict1];

    NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    [ar sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];
    NSLog(@"---%@",ar);

Comments

1

Put each row into a dictionary, and out those dictionaries into an array. Then sort your away using a predicate or sort block. If you want an array containing just the sorted names for example, you could use [ array valueForKeyPath:@"name" ]

Array looks like:

[
    { @"name" : ..., 
        @"birthdate" : ...birthdate..., 
        @"remaining" : ...days remaining... } ,
    {...},
    {...}
]

Comments

1

such as your MutableArray is a Dictionarys array , you can use sortUsingComparator to sort the array

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    int a = [(NSNumber *)[(NSDictionary *)obj1 objectiveForKey: @"remanning"] intValue];
    int b = [(NSNumber *)[(NSDictionary *)obj2 objectiveForKey: @"remanning"] intValue];

    if (a < b) {
        return NSOrderedAscending;
    }
    else if(a == b)
    {
        return NSOrderedSame;
    }
    return NSOrderedDescending;
}];

For example , I have a test :

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@(30),@(20),@(5),@(100), nil];

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    int a = [(NSNumber *)obj1 intValue];
    int b = [(NSNumber *)obj2 intValue];

    if (a < b) {
        return NSOrderedAscending;
    }
    else if(a == b)
    {
        return NSOrderedSame;
    }
    return NSOrderedDescending;
}];

[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@ , %d",obj,idx);
}];

then the output is : enter image description here

2 Comments

what about names and birthdates array how they will get changed in same way the changes are happening in remain array?
@user2189388 you should make a person is a Dictionary or a Class such as , persion1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Abhi Shah",@"01/14",@(300),@"names",@"birthdate",@"remanning", nil]; array = [NSMutableArray arrayWithObjects:persion1,persion2,persion3,..., nil]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.