Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
Objective c 如何对NSMutableArray重复值进行计数和排序_Objective C_Sorting_Nsmutablearray_Nsarray - Fatal编程技术网

Objective c 如何对NSMutableArray重复值进行计数和排序

Objective c 如何对NSMutableArray重复值进行计数和排序,objective-c,sorting,nsmutablearray,nsarray,Objective C,Sorting,Nsmutablearray,Nsarray,我看到了这个问题: 这个解决方案非常接近我所需要的 我指的解决方案是: NSInteger countedSort(id obj1, id obj2, void *context) { NSCountedSet *countedSet = context; NSUInteger obj1Count = [countedSet countForObject:obj1]; NSUInteger obj2Count = [countedSet countForObject:obj2

我看到了这个问题:

这个解决方案非常接近我所需要的

我指的解决方案是:

NSInteger countedSort(id obj1, id obj2, void *context) {
   NSCountedSet *countedSet = context;
   NSUInteger obj1Count = [countedSet countForObject:obj1];
   NSUInteger obj2Count = [countedSet countForObject:obj2];

   if (obj1Count > obj2Count) return NSOrderedAscending;
   else if (obj1Count < obj2Count) return NSOrderedDescending;
   return NSOrderedSame;
}    

排序按计数返回正确的排序数组,但我需要将计数包括在内或包含在另一个按升序排序的数组中。

解决方案可以是简单地查询任何对象的计数的计数集,以构建跟踪计数的数组:

// This after having sorted the array:
NSMutableArray* counts= [NSMutableArray arrayWithCapacity: array.count];
for(id object in array) {
    [counts addObject: @([countedSet countForObject: object]) ];
}

解决方案可以是简单地查询任何对象的计数集,以构建跟踪计数的数组:

// This after having sorted the array:
NSMutableArray* counts= [NSMutableArray arrayWithCapacity: array.count];
for(id object in array) {
    [counts addObject: @([countedSet countForObject: object]) ];
}

这将返回按最常出现的值排序的排序数组

NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];

NSArray *sortedValues = [countedSet.allObjects sortedArrayUsingComparator:^(id obj1, id obj2) {
    NSUInteger n = [countedSet countForObject:obj1];
    NSUInteger m = [countedSet countForObject:obj2];
    return (n <= m)? (n < m)? NSOrderedDescending : NSOrderedSame : NSOrderedAscending;
}];
NSCountedSet*countedSet=[[NSCountedSet alloc]initWithArray:array];
NSArray*sortedValues=[countedSet.AllObject SortedArray使用比较器:^(id obj1,id obj2){
NSUN整数=[countedSet countForObject:obj1];
NSUM整数=[countedSet countForObject:obj2];

return(n此返回按最常出现的值排序的排序数组

NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];

NSArray *sortedValues = [countedSet.allObjects sortedArrayUsingComparator:^(id obj1, id obj2) {
    NSUInteger n = [countedSet countForObject:obj1];
    NSUInteger m = [countedSet countForObject:obj2];
    return (n <= m)? (n < m)? NSOrderedDescending : NSOrderedSame : NSOrderedAscending;
}];
NSCountedSet*countedSet=[[NSCountedSet alloc]initWithArray:array];
NSArray*sortedValues=[countedSet.AllObject SortedArray使用比较器:^(id obj1,id obj2){
NSUN整数=[countedSet countForObject:obj1];
NSUM整数=[countedSet countForObject:obj2];

返回(n)一个简单的解决方案。谢谢!一个简单的解决方案。谢谢!