Bri Bri
Bri Bri

Reputation: 2268

How to create a sorted NSArray from an NSSet that contains arbitrary objects?

I have an NSSet that contains many different types of objects. Typically it will contain some combination of NSDictionaries, NSStrings, NSArrays, and Classes, i.e. the objective-c "Class" type. I need a way to produce an NSArray containing all of the objects in this NSSet, but is sorted so that it will always be in the same order for any set containing the same objects. To be specific, by "same object" I mean by value, not by address. It doesn't matter how it's sorted, as long as it's consistent.

I haven't found a way to do this so far. I can't find any way of doing an ordered comparison between two arbitrary objects. Does anyone have any suggestions on how to accomplish this?

Upvotes: 2

Views: 280

Answers (1)

trojanfoe
trojanfoe

Reputation: 122401

All objects are derived from NSObject, so you could simply sort by [NSObject description] (reference) which will be a summary of the object contents.

EDIT: As pointed out by @Paul.s in the comment; hash (reference) is the way to go (it will be quicker to compare).

Upvotes: 2

Related Questions