How do I sort an NSMutableArray with custom objects in it?

前端 未结 27 3314
予麋鹿
予麋鹿 2020-11-21 04:45

What I want to do seems pretty simple, but I can\'t find any answers on the web. I have an NSMutableArray of objects, and let\'s say they are \'Person\' objects

27条回答
  •  不知归路
    2020-11-21 05:14

    Your Person objects need to implement a method, say compare: which takes another Person object, and return NSComparisonResult according to the relationship between the 2 objects.

    Then you would call sortedArrayUsingSelector: with @selector(compare:) and it should be done.

    There are other ways, but as far as I know there is no Cocoa-equiv of the Comparable interface. Using sortedArrayUsingSelector: is probably the most painless way to do it.

提交回复
热议问题