我有三个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 也应该以同样的方式重新排序。
请参阅以下示例:
name birthdate remaining
"Aysu Can", "04/14/1992", 25
"Chirag Pandya" "10/07/1987" 201
"etc..."
答案 0 :(得分: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
答案 1 :(得分: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;
}
答案 2 :(得分: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);
答案 3 :(得分:1)
将每一行放入字典中,然后将这些字典放入数组中。然后使用谓词或排序块对您进行排序。例如,如果您想要一个仅包含已排序名称的数组,则可以使用[ array valueForKeyPath:@"name" ]
数组看起来像:
[
{ @"name" : ...,
@"birthdate" : ...birthdate...,
@"remaining" : ...days remaining... } ,
{...},
{...}
]
答案 4 :(得分:1)
,例如MutableArray
是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);
}];
然后输出是: