如何排序包含日期对象的自定义数组

时间:2015-10-10 11:29:00

标签: ios objective-c nsdate nspredicate nssortdescriptor

我有一个NSArray,它包含这样的自定义对象:

NSArray *array = {
 students,students

}

学生对象轮流存储价值如下:

student.name,
student.class,
student.admissionDate
student.school ..etc

现在我想要一个NSArray,其中包含根据admissionDate排序的所有学生的详细信息。

我尝试使用NSSortDecriptor,但这对我没有帮助。

修改

经过一番努力,我成功地形成了NSDictionary的NSMutable数组,看起来像是:

      for(Student *arr in array)
{

     NSMutableDictionary *dict;
    dict = [[NSMutableDictionary alloc]init];
    [dict setObject:arr.stuName forKey:@"name"];
    [dict setObject:arr.stuAdate forKey:@"date"];
    [dict setObject:arr.stuClass    forKey:@"class"];

    [expenseArray addObject:dict];
}



Printing description of expenseArray:
<__NSArrayM 0x7fe703833f70>(
{
 name = uuu;
 Adate = "2015-10-10 10:56:03 +0000";
 class = 1st;
},
{
  name = abc;
   Adate = "2015-10-07 11:10:00 +0000";
  class = 3rd;
},
 {
   name = btw;
   Adate = "2015-10-10 11:13:47 +0000";
   class = 4th;
 }
  )

现在我如何根据日期排序

4 个答案:

答案 0 :(得分:1)

排序描述符应该适合你

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"admissionDate" ascending:YES];
NSArray *sortedStudents = [studentArray sortedArrayUsingDescriptors:@[sortDescriptor]];

以下是工作示例

NSMutableArray *studentArray = [NSMutableArray new];

for (int i = 0; i < 8; i++) {
    Student *student = [Student new];

    unsigned int randomInterval = arc4random();
    student.admissionDate = [NSDate dateWithTimeIntervalSinceNow:randomInterval];
    [studentArray addObject:student];
}


for (Student *student in studentArray) {
    NSLog(@"%@", student.admissionDate);
}


NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"admissionDate" ascending:YES];
NSArray *sortedStudents = [studentArray sortedArrayUsingDescriptors:@[sortDescriptor]];

NSLog(@"\n\n * * * * * After Sorting * * * * * *\n\n");

for (Student *student in sortedStudents) {
    NSLog(@"%@", student.admissionDate);
}

<强>日志

2024-02-11 23:47:47 +0000
2093-11-13 21:49:48 +0000
2042-04-06 23:53:28 +0000
2032-12-23 01:49:46 +0000
2102-12-28 23:08:06 +0000
2058-10-17 14:14:27 +0000
2142-02-01 07:19:34 +0000
2048-05-14 07:07:04 +0000

* * * * * After Sorting * * * * * *

2024-02-11 23:47:47 +0000
2032-12-23 01:49:46 +0000
2042-04-06 23:53:28 +0000
2048-05-14 07:07:04 +0000
2058-10-17 14:14:27 +0000
2093-11-13 21:49:48 +0000
2102-12-28 23:08:06 +0000
2142-02-01 07:19:34 +0000

答案 1 :(得分:0)

排序描述符应该可以正常工作

NSSortDescriptor *admissionSort = [NSSortDescriptor sortDescriptorWithKey:@"Adate" ascending:NO];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:@[admissionSort]];

将根据录取日期对数组进行排序,假设它实际上是NSDate值。

答案 2 :(得分:0)

尽管NSSortDescription似乎是对数组进行排序的最佳方法,但这里有另一种方法:

NSArray *studentsArray = @[someStudent1, someStudent2];
NSArray *sortedStudentsArray = [studentsArray sortedArrayUsingComparator:^NSComparisonResult(Student *student1, Student *student2) {
    return [student1.admissionDate compare:student2.admissionDate];
}];

答案 3 :(得分:0)

NSSortDescriptor工作正常!

你应该用Student类替换你的可变字典。

@interface Student : NSObject

@property NSString *name;
@property NSString *className;
@property NSString *schoolName;
@property NSDate *admissionDate;

@end

@implementation Student


@end  

此类的使用

Student *student1 = [[Student alloc] init];
student1.name = @"Name a";
student1.className = @"4th";
student1.schoolName = @"XYZ High school";
student1.admissionDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24]; // 60 * 60 * 24 one day time interval in second  

Student *student2 = [[Student alloc] init];
student2.name = @"Name b";
student2.className = @"5th";
student2.schoolName = @"XYZ High school";
student2.admissionDate = [NSDate date]; // current date


NSArray *array = [NSArray arrayWithObjects:student1,student2, nil];


NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"admissionDate" ascending:YES];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:@[descriptor]];

for (Student *student in sortedArray) {
    NSLog(@"admissionDate %@", student.admissionDate);
}

请记住[array sortedArrayUsingDescriptors:@ [descriptor]]返回已排序的数组,而不是重新排列的数组元素。

希望这会奏效。