how to change order of NSMutable array in same way

2020-04-01 05:50发布

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..."

5条回答
混吃等死
2楼-- · 2020-04-01 06:13

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

查看更多
冷血范
3楼-- · 2020-04-01 06:17

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... } ,
    {...},
    {...}
]
查看更多
神经病院院长
4楼-- · 2020-04-01 06:18
    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;
}
查看更多
狗以群分
5楼-- · 2020-04-01 06:31

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
查看更多
贼婆χ
6楼-- · 2020-04-01 06:36

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);
查看更多
登录 后发表回答