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

前端 未结 27 3901
予麋鹿
予麋鹿 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:09

    You can use the following generic method for your purpose. It should solve your issue.

    //Called method
    -(NSMutableArray*)sortArrayList:(NSMutableArray*)arrDeviceList filterKeyName:(NSString*)sortKeyName ascending:(BOOL)isAscending{
        NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:sortKeyName ascending:isAscending];
        [arrDeviceList sortUsingDescriptors:[NSArray arrayWithObject:sorter]];
        return arrDeviceList;
    }
    
    //Calling method
    [self sortArrayList:arrSomeList filterKeyName:@"anything like date,name etc" ascending:YES];
    

提交回复
热议问题