2

I must be under some strange misconception ...

Consider:

NSSet * mySet = [NSSet setWithArray:@[@1, @9, @3]] ;
NSOrderedSet * mySortedSet = [NSOrderedSet orderedSetWithSet:mySet] ;

for (NSNumber * n in mySet) {
    NSLog(@"unsorted: %@", n) ;
}
for (NSNumber * n in mySortedSet) {
    NSLog(@"--sorted: %@", n) ;
}

And the console log:

2014-05-05 20:38:46.645 WoGa[50557:60b] unsorted: 9
2014-05-05 20:38:48.675 WoGa[50557:60b] unsorted: 1
2014-05-05 20:38:49.875 WoGa[50557:60b] unsorted: 3
2014-05-05 20:38:54.277 WoGa[50557:60b] --sorted: 9
2014-05-05 20:38:55.544 WoGa[50557:60b] --sorted: 1
2014-05-05 20:38:56.684 WoGa[50557:60b] --sorted: 3

Huh? How is that 'ordered' ? I have no problem sorting myself if needs be, but what exactly is an NSOrderedSet then?

I can accept that the order of the elements in mySet is unpredictable. But what is [NSOrderedSet orderedSetWithSet:mySet] supposed to achieve if not sorting in some order or other ?

From the documentation:

You can use ordered sets as an alternative to arrays when the order of elements is important and performance in testing whether an object is contained in the set is a consideration—testing for membership of an array is slower than testing for membership of a set.

Again, what is the order that NSOrderedSet orderedSetWithSet: is supposed to provide you with?

verec
  • 5,224
  • 5
  • 33
  • 40
  • Look at what you’re doing: taking an (ordered) array, turning it into an (unordered) set, and then turning that into an ordered set. Of course you’re not going to end up with an ordered set with the same order as your original array! Use `orderedSetWithArray:` instead. – bdesham May 05 '14 at 20:02
  • 1. The initial array was NOT sorted, on purpose. Read the snippet again. 2. Calling a class WTFOrderWTF with a WTFOrderFromWTF and _not_ providing an order is, at best, misleading, no? When you _sort_ things you sort them into an _order_. What is the _order_ that `orderedSetWithSet` claims to provide? – verec May 05 '14 at 20:05
  • 1
    Whatever order you give it. If you had called `orderedSetWithArray:@[@1, @9, @3]` then you would have received an ordered set with the numbers 1, 9, and 3 *in that order*. You seem to be misunderstanding the difference between sorting and ordering. [This SO question](http://stackoverflow.com/q/1084146/371228) has more information. – bdesham May 05 '14 at 20:19

1 Answers1

3

You are confusing "ordered" and "sorted". An array is an ordered collection, which means it has a first, second, third, up to a last element. A set or dictionary is an unordered collection, with no ordering of the elements. No first or last element.

The NSOrderedSet combines the properties of NSSet and NSArray. Like an array, you can add elements at the end or insert them anywhere in the array, or remove elements anywhere, or sort the elements. Like an NSSet, you can look up whether an element is in already present in constant time.

Ordered collections can be sorted, but they are not automatically sorted. Unordered collections cannot be sorted, because they are not ordered.

On the other hand, you could have just read the documentation of NSOrderedSet which makes this all quite clear.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • How is "orderedSetWithSet: Creates and returns an ordered set with the contents of a set." clear about the difference between sorted and ordered, please? – verec May 05 '14 at 20:02
  • 2
    It says "ordered". It doesn't say "sorted". If you want "sorted", you go into NSMutableOrderedSet and look at the sort methods. The word "ordered" vs. "unordered" has a commonly known meaning, which isn't "sorted". – gnasher729 May 05 '14 at 21:02
  • @gnasher729: How is it possible to have both ordered and fast lookup functionality at one place? I am working on similar problem.. where can I find the implementation details of NSOrderedSet?? – Vinay Jun 04 '14 at 08:24