0

我有三个arrays。它们是姓名生日剩余天数,如下所示:

    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

我想将剩余的日子重新排列成array升序,但同时姓名出生日期也应该以同样的方式重新排列。

请参阅下面的示例:

    name                         birthdate                 remaining 

    "Aysu Can",                  "04/14/1992",             25
    "Chirag Pandya"              "10/07/1987"              201
    "etc..."
4

5 回答 5

3

用于NSMutableDictionary将数据存储到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];
 }

在这里安排名称数组后,使用搜索选项使用名称不使用索引并使用NSPredicate搜索数据NSMutableArray

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"names matches[cd] %@", name];
 NSArray *result = [array filteredArrayUsingPredicate:predicate];
 NSMutableDictionary *dict = [[[result objectAtIndex:0] mutableCopy] autorelease];
 NSLog(@"%@",dict);// result
于 2013-03-20T07:25:22.493 回答
2

一旦像这样尝试它会帮助你,

 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);
于 2013-03-20T11:10:09.133 回答
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]];

在 tableView 列表中使用它

- (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;
}
于 2013-03-20T08:49:28.350 回答
1

比如你的MutableArrayis a Dictionarys array,你可以sortUsingComparator 用来对数组进行排序

[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;
}];

例如,我有一个测试:

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);
}];

那么输出是: 在此处输入图像描述

于 2013-03-20T06:59:31.930 回答
1

将每一行放入字典中,然后将这些字典放入数组中。然后使用谓词或排序块对您进行排序。例如,如果您想要一个仅包含排序名称的数组,您可以使用[ array valueForKeyPath:@"name" ]

数组看起来像:

[
    { @"name" : ..., 
        @"birthdate" : ...birthdate..., 
        @"remaining" : ...days remaining... } ,
    {...},
    {...}
]
于 2013-03-20T06:50:55.723 回答