如何对包含NSDates的数组进行排序

时间:2015-12-29 09:39:57

标签: ios arrays sorting

你能帮我排序(升序)数组包含regex

NSDate

5 个答案:

答案 0 :(得分:1)

由于NSDate响应compare且默认排序顺序为升序,因此最简单的方法是

[tempArray sortUsingSelector:@selector(compare:)];

答案 1 :(得分:1)

使用以下代码:

  [your array sortUsingComparator:
  ^NSComparisonResult(id obj1, id obj2){

         obj1 *o1 = (obj1*)obj1;
         obj2 *o2 = (obj2*)obj2;
         if (o1.personAge > 02.personAge) {
            return (NSComparisonResult)NSOrderedDescending;
        }


        return (NSComparisonResult)NSOrderedSame;
}

];

答案 2 :(得分:0)

NSSortDescriptor *dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self.dueDate" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSDate *date1 = [dateFormat dateFromString:obj1];
    NSDate *date2 = [dateFormat dateFromString:obj2];

    return [date1 compare:date2];
}];

答案 3 :(得分:0)

最好的方法是使用排序描述符。

<TextBlock Text="{Binding Name}"
           ToolTip="{Binding Path=Name, StringFormat='The name is: {0}{}'}"/>

答案 4 :(得分:0)

[array1 sortUsingComparator: (NSComparator)^(NSString *key1, NSString *key2)
 {        
     return [key2 compare: key1];
 }
 ];

如果你想要数组按升序或降序改变

  

比较

return [key1 compare: key2];