I have a NSArray of ‘generic Objects’ which contain the following properties
-name
-id
-type (question, topic or user)
How can I sort this array of generic object based on the generic object’s type? E.g. I want to display all generic objects of type ‘topic’ on the top, followed by ‘users’ than ‘questions’
You’ll need to define a custom sorting function, then pass it to an NSArray method that allows custom sorting. For example, using
sortedArrayUsingFunction:context:, you might write (assuming your types are NSString instances):If your types aren’t NSStrings, then just adapt the function as needed – you could replace the strings in the
orderarray with your actual objects, or (if your types are part of an enumeration) do direct comparison and eliminate theorderarray entirely.