Sorting an array of Objective-c objects

后端 未结 1 1592
春和景丽
春和景丽 2021-02-06 04:40

So I have a custom class Foo that has a number of members:

@interface Foo : NSObject {
    NSString *title;
    BOOL     taken;
    NSDate   *dateCr         


        
相关标签:
1条回答
  • 2021-02-06 05:29

    That's quite simple to do.

    First, in your Foo object, create a method

    - (NSComparisonResult) compareWithAnotherFoo:(Foo*) anotherFoo;
    

    Which will return

    [[self dateCreated] compare:[anotherFoo dateCreated]];
    

    In the end, call on the array

    [yourArray sortUsingSelector:@selector(compareWithAnotherFoo:)];
    

    Hope this helps, Paul

    0 讨论(0)
提交回复
热议问题