Objective C: Sort Two Dimensional Array

前端 未结 2 363
青春惊慌失措
青春惊慌失措 2021-01-07 10:15

I have an array of arrays. The contained array\'s first elements are all NSDate objects. I would like to sort the array containing the arrays in order from most recent to le

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 10:20

    If array is mutable and you want to sort it in place:

    [array sortUsingComparator:^(id a, id b) {
        return [b[0] compare:a[0]];
    }];
    

    If array is immutable or you want to leave it alone and make a sorted copy:

    NSArray *sortedArray = [array sortedArrayUsingComparator:^(id a, id b) {
        return [b[0] compare:a[0]];
    }];
    

提交回复
热议问题