关于ios:如何通过时间比较按升序对nsmutablearray进行排序

how to sort nsmutablearray in ascending order with time comparison

本问题已经有最佳答案,请猛点这里访问。

我有NSmutableArray类型的时间数组。我想按升序排列这个数组。这些是数组中的对象

1
2
3
4
5
6
7
8
9
10
11
12
13
        "9:30" at index 0
        "8:30" at index 1
        "8:45" at index 2
        "6:45" at index 3
        "7:30"  at index 4

and i want to sort this time array like this order

        "6:45" at index 0
        "7:30" at index 1
        "8:30" at index 2
        "8:45" at index 3
        "9:30" at index 4

我花了一整天的时间准备算法,但没能成功


可以使用以下代码按升序对数组进行排序:

1
2
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
return [myArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];


请使用以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  NSMutableArray *listOfTime = [[NSMutableArray alloc]      initWithObjects:@"9:30",@"8:30",@"8:45",@"6:45",@"7:30", nil];

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];

NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];

NSArray *sortedArray = [listOfTime sortedArrayUsingDescriptors:sorters];


NSMutableArray *sortArray = [[NSMutableArray alloc] init];

[sortArray addObjectsFromArray:sortedArray];

NSLog(@"sortArray : %@",sortArray);