1

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

4
  • sorry i missed let me update the questiton Commented Oct 10, 2015 at 11:39
  • What's in admissionDate? a NSDate a NSString? Commented Oct 10, 2015 at 11:43
  • @Larme It stores NSDate Commented Oct 10, 2015 at 11:49
  • @user5370850 Please don't forget to make correct answer. Everybody has put time to read and think about your problem. Commented Oct 11, 2015 at 3:40

4 Answers 4

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.