NSUser
NSUser

Reputation: 1233

How to sort Custom array which contains date object

I have an NSArray which contains custom objects like this:

NSArray *array = {
 students,students

}

And student object in turns store values like :

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

Now I want an NSArray which contains all the student's detail sorted in based on their admissionDate.

I tried using NSSortDecriptor but it doesn't helped me.

EDIT

After some hard work I have successfully formed a NSMutable array of NSDictionary which Looks like:

      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;
 }
  )

Now how can i sort based on date

Upvotes: 1

Views: 80

Answers (4)

Shamim Hossain
Shamim Hossain

Reputation: 1760

NSSortDescriptor works fine!

You should replace your mutable dictionary with Student class.

@interface Student : NSObject

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

@end

@implementation Student


@end  

Uses of this class

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);
}

Please keep in mind that [array sortedArrayUsingDescriptors:@[descriptor]] return sorted array, not rearranged array elements itself.

Hope this will work.

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

The sort descriptor should work for you

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

Below is the working example

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);
}

Logs

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

Upvotes: 1

psci
psci

Reputation: 933

Although NSSortDescription seems to be the best way to sort an array, here is alternative approach:

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

Upvotes: 0

Joris Kluivers
Joris Kluivers

Reputation: 12081

A sort descriptor should work fine

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

will sort the array based on the admission date assuming that is actually a NSDate value.

Upvotes: 0

Related Questions