Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用值对NSDictionary的NSArray进行排序_Ios_Objective C_Nsarray_Nsdictionary - Fatal编程技术网

Ios 使用值对NSDictionary的NSArray进行排序

Ios 使用值对NSDictionary的NSArray进行排序,ios,objective-c,nsarray,nsdictionary,Ios,Objective C,Nsarray,Nsdictionary,我有一大堆字典。在NSDictionary中,我有一个名为“rowID”的元素,它保存为NSString。但是,其中保存的所有内容都是数字,即使它们保存为字符串 我想知道如何根据这个值对数组进行排序,但排序方式是1-100。目前,当我对它进行排序时,当它应该是1-10时,10会排在第一位 我是这样分类的: NSArray *tempSortedItemsArray = [itemArray sortedArrayUsingDescriptors:

我有一大堆字典。在NSDictionary中,我有一个名为“rowID”的元素,它保存为NSString。但是,其中保存的所有内容都是数字,即使它们保存为字符串

我想知道如何根据这个值对数组进行排序,但排序方式是1-100。目前,当我对它进行排序时,当它应该是1-10时,10会排在第一位

我是这样分类的:

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

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

现在的问题是要传递哪个选择器。您可以在NSString上创建一个类别,实现一个自定义排序函数,该函数根据字符串的数值进行排序。请记住,从文件中可以看出:

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

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

试试这个:-

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)];
}];
试着这样做:-

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

您可以通过以下代码获得数字排序:-

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

这与用户已经在做的有什么不同???更新仍然不正确。这是字母排序,而不是所需的数字排序。这是不正确的。OP声明Rowid是NSString对象,而不是NSNumber对象。这将导致同样的不正确字符串比较。在我结束时,它工作正常。如果您发现不正确的结果,则测试它,然后我将删除我的应答。如果rowID值实际上是NSNumber对象,但OP有NSString对象,则这是正确的。这对我有效。。。这些值是字符串。。。为什么它对我有用?因为数字搜索这正是你想要的。。
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);