I have an NSArray of objects that all subclass with their different types from a more abstract class.
I would like to sort the array by the object’s class type. For example:
NSArray with CLASSA CLASSB AND CLASS C subclassed from CLASSz
myArray = {C, B, A}
Sort myArray decending: {A, B, C}
I see sort descriptors and that seems like it’s the right path but I am comparing isKindOfClass which is not a property so can not be used in the NSSortDescriptiors thing.
Thanks for any help.
Well, the biggest hurdle I see is that you’re probably going to need a custom property on
Classobjects which you could use as the key of anNSSortDescriptor. Unfortunately, there’s no real way to add methods/categories toClassobjects.So what I would probably do is use
NSStringFromClass()to convert yourClassobjects into strings, then use anNSStringcategory to add a custom comparator method (something likecompareToClass:) that converts the string back to aClass, does the subclass determination, and then returns the appropriate ordering constant.From there, you can use
NSArray‘ssortedArrayUsingSelector:method to get them in order.