objective-c 对 NSSet 进行排序的最有效方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1066829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:49:20  来源:igfitidea点击:

What is the most efficient way to sort an NSSet?

objective-ccocoadata-structuressortingnsset

提问by Boon

What's the most efficient way to sort objects in an NSSet/NSMutableSetbased on a property of the objects in the set? Right now the way I am doing it is by iterating through each object, add them to a NSMutableArray, and sort that array with NSSortDescriptor.

根据集合中对象的属性对NSSet/ 中NSMutableSet的对象进行排序的最有效方法是什么?现在我的做法是遍历每个对象,将它们添加到 a NSMutableArray,然后用NSSortDescriptor.

回答by cobbal

try using

尝试使用

[[mySet allObjects] sortedArrayUsingDescriptors:descriptors];

Edit: For iOS ≥ 4.0 and Mac OS X ≥ 10.6 you can directly use

编辑:对于 iOS ≥ 4.0 和 Mac OS X ≥ 10.6 可以直接使用

[mySet sortedArrayUsingDescriptors:descriptors];

回答by Quinn Taylor

The "most efficient way" to sort a set of objects varies based on what you actually mean. The casual assumption (which the previous answers make) is a one-time sort of objects in a set. In this case, I'd say it's pretty much a toss-up between what @cobbalsuggests and what you came up with — probably something like the following:

对一组对象进行排序的“最有效方法”因您的实际意思而异。随意假设(先前的答案做出的)是集合中的一次性对象。在这种情况下,我会说在@cobbal 的建议和您提出的建议之间几乎是一种折腾——可能类似于以下内容:

NSMutableArray* array = [NSMutableArray arrayWithCapacity:[set count]];
for (id anObject in set)
    [array addObject:anObject];
[array sortUsingDescriptors:descriptors];

(I say it's a toss-up because @cobbal's approach creates two autoreleased arrays, so the memory footprint doubles. This is inconsequential for small sets of objects, but technically, neither approach is very efficient.)

(我说这是一个折腾,因为@cobbal 的方法创建了两个自动释放的数组,所以内存占用加倍。这对于小对象集来说无关紧要,但从技术上讲,这两种方法都不是很有效。)

However, if you're sorting the elements in the set more than once (and especially if it's a regular thing) this is definitely not an efficient approach. You could keep an NSMutableArray around and keep it synchronized with the NSSet, then call -sortUsingDescriptors: each time, but even if the array is already sorted it will still require N comparisons.

但是,如果您不止一次对集合中的元素进行排序(尤其是如果它是常规的事情),这绝对不是一种有效的方法。你可以保留一个 NSMutableArray 并保持它与 NSSet 同步,然后每次调用 -sortUsingDescriptors:,但即使数组已经排序,它仍然需要 N 次比较。

Cocoa by itself just doesn't provide an efficient approach for maintaining a collection in sorted order. Java has a TreeSetclass which maintains the elements in sorted order whenever an object is inserted or removed, but Cocoa does not. It was precisely this problem that drove me to develop something similar for my own use.

Cocoa 本身并不能提供一种以排序顺序维护集合的有效方法。Java 有一个TreeSet类,它在插入或删除对象时按排序顺序维护元素,但 Cocoa 没有。正是这个问题驱使我开发类似的东西供我自己使用。

As part of a data structures framework I inherited and revamped, I created a protocol and a few implementations for sorted sets. Any of the concrete subclasses will maintain a set of distinct objects in sorted order. There are still refinements to be made — the foremost being that it sorts based on the result of -compare: (which each object in the set must implement) and doesn't yet accept an NSSortDescriptor. (A workaround is to implement -compare: to compare the property of interest on the objects.)

作为我继承和改造的数据结构框架的一部分,我为 sorted set创建了一个协议和一些实现。任何具体的子类都将按排序顺序维护一组不同的对象。还有一些改进需要做——最重要的是它根据 -compare:(集合中的每个对象都必须实现)的结果进行排序,并且还不接受 NSSortDescriptor。(解决方法是实现 -compare: 比较对象上感兴趣的属性。)

One possible drawback is that these classes are (currently) not subclasses of NS(Mutable)Set, so if you must pass an NSSet, it won't be ordered. (The protocol does have a -set method which returns an NSSet, which is of course unordered.) I plan to rectify that soon, as I've done with the NSMutableDictionary subclasses in the framework. Feedback is definitely welcome. :-)

一个可能的缺点是这些类(当前)不是 NS(Mutable)Set 的子类,因此如果您必须传递 NSSet,则不会对其进行排序。(该协议确实有一个返回 NSSet 的 -set 方法,这当然是无序的。)我计划尽快纠正这个问题,就像我在框架中处理 NSMutableDictionary 子类一样。绝对欢迎反馈。:-)

回答by bioffe

For iOS ≥ 5.0 and Mac OS X ≥ 10.7 you can directly use NSOrderedSet

对于 iOS ≥ 5.0 和 Mac OS X ≥ 10.7 可以直接使用 NSOrderedSet

回答by stefanB

NSSet is a collection of unordered objects. Looking at apple references Arrays are ordered collections.

NSSet 是无序对象的集合。查看苹果引用数组是有序集合。

Looking at NSArray there is a discussion with examples of sorting at http://developer.apple.com/documentation/Cocoa/Conceptual/Collections/Articles/sortingFilteringArrays ...

查看 NSArray 有一个关于排序示例的讨论 http://developer.apple.com/documentation/Cocoa/Conceptual/Collections/Articles/sortingFilteringArrays ...

Example from the link:

来自链接的示例:

NSInteger alphabeticSort(id string1, id string2, void *reverse)
{
    if (*(BOOL *)reverse == YES) {
        return [string2 localizedCaseInsensitiveCompare:string1];
    }
    return [string1 localizedCaseInsensitiveCompare:string2];
}


// assuming anArray is array of unsorted strings

NSArray *sortedArray;

// sort using a selector
sortedArray =
    [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

// sort using a function
BOOL reverseSort = NO;
sortedArray =
    [anArray sortedArrayUsingFunction:alphabeticSort context:&reverseSort];

回答by iTux

You can't sort NSSet, because "sortedArrayUsingFunction:" set result as NSArray... And all upper hint work with only Array :)

你不能对 NSSet 进行排序,因为 "sortedArrayUsingFunction:" 将结果设置为 NSArray ......并且所有上层提示仅适用于 Array :)

NSArray *myArray = [mySet sortedArrayUsingDescriptors:descriptors];

Work perfect, and not need other way :)

工作完美,不需要其他方式:)

回答by Andriy

Since OS X 10.7 and iOS 5.0 there's NSOrderedSet. You can use it to keep objects in set and keep their order. NSMutableOrderedSethas methods for sorting. In some situations this may give a performance improvement, since you don't have to create separate object like NSArrayto store sorted items.

从 OS X 10.7 和 iOS 5.0 开始,NSOrderedSet. 您可以使用它来保持对象的设置并保持它们的顺序。NSMutableOrderedSet有排序方法。在某些情况下,这可能会提高性能,因为您不必创建单独的对象NSArray来存储已排序的项目。