I have NSMutableArray “days” which contain 7 NSMutableArrays,7 NSMutableArrays contain class Lesson.
@interface Lesson : NSObject <NSCoding>{
NSString *time1;
NSString *time2;
NSString *predmet;
NSString *namPrepod;
NSString *zamet;
}
Before I was doing sort with sortedArrayUsingComparator, but now I must do sort with sortedArrayUsingDescriptors;
sortedArrayUsingComparator look like
chetNedel.sunday = [NSMutableArray arrayWithArray:[chetNedel.sunday sortedArrayUsingComparator:^(id cont1, id cont2) { return [[(Lesson *) cont1 time1] compare:[(Lesson *) cont2 time1]]; }]];
[chetNedel.days removeObjectAtIndex:0];
[chetNedel.days insertObject:chetNedel.sunday atIndex:0];
how will be sorted with sortedArrayUsingDescriptors?
This is the method you’ll use on the
NSMutableArrayyou want to sort.So you want to sort an array of Lesson objects. The
NSArrayparameter is going to be an array ofNSSortDescriptorobjects.An NSSortDescriptor describes how you’re going to sort an object based off it’s properties. So in your case of sorting Lesson objects, you’re going to sort on some or all of the string properties you have.
This is a sort descriptor for your Lesson object, we’ll be sorting them on their time1 property.
The key parameter is your objects property you want to compare (or you could use time2, premedt, ect). The ascending property is a boolean on whether you want the sorted values to be returned in an increasing or decreasing order. The selector property is a method that will be used to compare the properties of your object.
Now back to the
NSArrayof sort descriptors. At this point you’d start constructing your array of sort descriptors.And pass this array of sort descriptors to the original method:
- (void)sortUsingDescriptors:(NSArray *)sortDescriptorsYour original array of lessons is now sorted.