You have to convert the Set to an Array.
The reason for this is the following definition:
"Sets are different in the sense that order does not matter and these
will be used in cases where order does not matter."
Whereas a set:
"... stores distinct values of the same type in a collection with no
defined ordering."
See for further information:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html
In this case you will have a list of distinct values (and I consider your decision to use a NSSet as valid argument) you will have to transform your set to an array, you should not run into trouble, as your set already seems to take care of that your objects are of the same type (e.g. "Fruit").
So in this case, we will have
- Define Sort Criteria
- Sort the Array
I have attached a sample for both Objective-C and Swift, in case you need the one way or the other:
Objective-C Code
NSMutableSet<Fruit> *uniqueFruits = [NSMutableSet new];
[uniqueFruits addObject:[[Fruit alloc] initWithName:@"Apple"]];
[uniqueFruits addObject:[[Fruit alloc] initWithName:@"Banana"]];
[uniqueFruits addObject:[[Fruit alloc] initWithName:@"Orange"]];
// 1 Define Sort Criteria
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"size" ascending:YES]; // Key is the NSString of a certain selector. If it is an attribute of another class reference. Simply use "reference.property".
// 2 Sort the Array
NSArray<Fruit> *sortedArray = [self.uniqueFruits sortedArrayUsingDescriptors:@[descriptor]];
Swift 3 Code
let uniqueFruits = Set<Fruit>(Fruit("Apple"), Fruit("Banana"), Fruit("Orange"))
// 1 & 2 Define Sort Criteria and sort the array, using a trailing closure that sorts on a field/particular property you specify
// Be aware: result is an array
let sortedArray = uniqueFruits.sort({ $0.size < $1.size })