使用值对NSDictionary的NSArray进行排序

时间:2014-02-15 05:15:57

标签: ios objective-c nsarray nsdictionary

我有NSArray NSDictionaries,在NSDictionary我有一个名为'rowID'的元素,保存为NSString,但保存在其中的所有内容都是一个数字..但仍然是一个字符串值。

我想知道如何根据这个值对数组进行排序,但是在排序1-100方面,目前当我对它进行排序时,首先是10.当它应该是1-10时。

这就是我的排序方式:

NSArray *tempSortedItemsArray = [itemArray sortedArrayUsingDescriptors:
                                     @[[NSSortDescriptor
                                        sortDescriptorWithKey:@"rowID" ascending:YES]]];

4 个答案:

答案 0 :(得分:6)

试试这个: -

NSArray *aSortedArray = [itemArray sortedArrayUsingComparator:^(NSMutableDictionary *obj1,NSMutableDictionary *obj2) {
    NSString *num1 =[obj1 objectForKey:@"rowID"];
    NSString *num2 =[obj2 objectForKey:@"rowID"];
    return (NSComparisonResult) [num1 compare:num2 options:(NSNumericSearch)];
}];

答案 1 :(得分:2)

您可以按照以下代码进行数字排序: -

NSMutableArray *tmpAr = [[NSMutableArray alloc] init];

NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
[tmpDict setObject:@"name" forKey:@"number5"];
[tmpDict setObject:@"5" forKey:@"id"];
[tmpAr addObject:tmpDict];

tmpDict = [[NSMutableDictionary alloc] init];
[tmpDict setObject:@"name" forKey:@"number3"];
[tmpDict setObject:@"3" forKey:@"id"];
[tmpAr addObject:tmpDict];

tmpDict = [[NSMutableDictionary alloc] init];
[tmpDict setObject:@"name" forKey:@"number2"];
[tmpDict setObject:@"2" forKey:@"id"];
[tmpAr addObject:tmpDict];

tmpDict = [[NSMutableDictionary alloc] init];
[tmpDict setObject:@"name" forKey:@"number1"];
[tmpDict setObject:@"1" forKey:@"id"];
[tmpAr addObject:tmpDict];

tmpDict = [[NSMutableDictionary alloc] init];
[tmpDict setObject:@"name" forKey:@"number4"];
[tmpDict setObject:@"4" forKey:@"id"];
[tmpAr addObject:tmpDict];

tmpDict = [[NSMutableDictionary alloc] init];
[tmpDict setObject:@"name" forKey:@"number25"];
[tmpDict setObject:@"25" forKey:@"id"];
[tmpAr addObject:tmpDict];

tmpDict = [[NSMutableDictionary alloc] init];
[tmpDict setObject:@"name" forKey:@"number10"];
[tmpDict setObject:@"10" forKey:@"id"];
[tmpAr addObject:tmpDict];

tmpDict = [[NSMutableDictionary alloc] init];
[tmpDict setObject:@"name" forKey:@"number7"];
[tmpDict setObject:@"7" forKey:@"id"];
[tmpAr addObject:tmpDict];

NSLog(@"tmpar1 = %@",tmpAr);

[tmpAr sortUsingComparator:
 ^(id obj1, id obj2)
 {
     NSInteger value1 = [[obj1 objectForKey: @"id"] intValue];
     NSInteger value2 = [[obj2 objectForKey: @"id"] intValue];
     if (value1 > value2)
     {
         return (NSComparisonResult)NSOrderedDescending;
     }

     if (value1 < value2)
     {
         return (NSComparisonResult)NSOrderedAscending;
     }
     return (NSComparisonResult)NSOrderedSame;
 }];

NSLog(@"tmpar2 = %@",tmpAr);

答案 2 :(得分:1)

如果使用initWithKey:ascending:selector:sortDescriptorWithKey:ascending:comparator:创建排序描述符,则可以指定用于比较对象的选择器。

现在问题是哪个选择器要传递。那么你可以在NSString上创建一个Category来实现一个自定义排序函数,该函数根据字符串的数值进行排序。记得从文件:

  

选择器必须指定由值实现的方法   keyPath标识的属性。用于比较的选择器   传递一个参数,该对象与self进行比较,以及   必须返回适当的NSComparisonResult常量。选择器   必须具有相同的方法签名:

- (NSComparisonResult)localizedCompare:(NSString *)aString
  

指定属性键的对象,相对于每个对象   集合,必须实现用于创建的比较选择器   排序描述符。如果未指定自定义选择器,则对象必须   实施比较:。

答案 3 :(得分:1)

试试这样: -

NSDictionary *row1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"rowId",nil];
NSDictionary *row2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2",@"rowId",nil];
NSDictionary *row3 = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"rowId",nil];

NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:row1,row2,row3,nil];

NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"rowId" ascending:YES];
[arr sortUsingDescriptors:[NSArray arrayWithObject:desc]];

// before sort
NSLog(@"Before %@",arr);
[arr sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
    NSString *first = [item1 objectForKey:@"rowId"];
    NSString *second = [item2 objectForKey:@"rowId"];
    return [first compare:second options:NSNumericSearch];
}];
// After sort
NSLog(@"After %@",arr);