简体   繁体   English

排序NSSet的最有效方法是什么?

[英]What is the most efficient way to sort an NSSet?

What's the most efficient way to sort objects in an NSSet / NSMutableSet based on a property of the objects in the set? 基于集合中对象的属性,在NSSet / NSMutableSet对对象进行排序的最有效方法是什么? 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 . 现在,我这样做的方法是迭代每个对象,将它们添加到NSMutableArray ,并使用NSSortDescriptor对该数组进行NSSortDescriptor

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 OSX≥10.6,您可以直接使用

[mySet sortedArrayUsingDescriptors:descriptors];

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 @cobbal suggests 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. Cocoa本身并不提供按排序顺序维护集合的有效方法。 Java has a TreeSet class which maintains the elements in sorted order whenever an object is inserted or removed, but Cocoa does not. Java有一个TreeSet类,只要插入或删除对象,它就按照排序顺序维护元素,但Cocoa却没有。 It was precisely this problem that drove me to develop something similar for my own use. 正是这个问题促使我为自己开发类似的东西。

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. 仍然需要进行改进 - 最重要的是它根据-compare的结果进行排序:(集合中的每个对象必须实现)并且还不接受NSSortDescriptor。 (A workaround is to implement -compare: to compare the property of interest on the objects.) (解决方法是实现-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. 一个可能的缺点是这些类(当前)不是NS(Mutable)Set的子类,因此如果必须传递NSSet,则不会对它进行排序。 (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. (该协议确实有一个-set方法,它返回一个NSSet,当然是无序的。)我打算很快纠正这个问题,因为我已经完成了框架中的NSMutableDictionary子类。 Feedback is definitely welcome. 绝对欢迎反馈。 :-) :-)

对于NSOrderedSet和Mac NSOrderedSet您可以直接使用NSOrderedSet

NSSet is a collection of unordered objects. NSSet是无序对象的集合。 Looking at apple references Arrays are ordered collections. 查看苹果参考数据是有序的集合。

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

Since OS X 10.7 and iOS 5.0 there's NSOrderedSet . 从OS X 10.7和iOS 5.0开始,有NSOrderedSet You can use it to keep objects in set and keep their order. 您可以使用它来保持对象集并保持其顺序。 NSMutableOrderedSet has methods for sorting. NSMutableOrderedSet具有排序方法。 In some situations this may give a performance improvement, since you don't have to create separate object like NSArray to store sorted items. 在某些情况下,这可能会提高性能,因为您不必创建单独的对象(如NSArray来存储已排序的项目。

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 :) 工作完美,而不需要其他方式:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSSortDescriptor对Core Data NSSet对象进行排序的最有效方法 - NSSortDescriptor most effective way to sort Core Data NSSet objects 以这种方式比较NSString的最有效方法是什么 - What is the most efficient way to compare an NSString in this way 向UIImageView添加反射的最有效方法是什么 - What is the most efficient way to add a reflection to a UIImageView 实现游戏计时器的最有效方法是什么 - What is the most efficient way of implementing a game timer 生成NSNumber序列的最有效方法是什么? - What is the most efficient way to generate a sequence of NSNumbers? 在iPhone上创建粒子的最有效方法是什么 - What's the most efficient way to create particles on the iPhone 带有过滤器的SQL大表视图-最有效的方法是什么? - A big tableview from SQL with filter - what's the most efficient way? Objective C — 枚举数组最快和最有效的方法是什么? - Objective C — What is the fastest and most efficient way to enumerate an array? 在 iOS 中显示多列表格的最有效方法是什么? - What is the most efficient way to display a table with multiple columns in iOS? 存储4个属性(NSPoints)位置的最有效方法是什么? - What is the most efficient way to store the location of 4 properties (NSPoints)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM