2

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?

1 Answer 1

3

This is the method you'll use on the NSMutableArray you want to sort.

- (void)sortUsingDescriptors:(NSArray *)sortDescriptors

So you want to sort an array of Lesson objects. The NSArray parameter is going to be an array of NSSortDescriptor objects.

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.

NSSortDescriptor *time1Descriptor = [[NSSortDescriptor alloc] initWithKey:@"time1" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

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 NSArray of sort descriptors. At this point you'd start constructing your array of sort descriptors.

NSArray *sortDescriptorArray = [[NSArray alloc] initWithObjects:time1Descriptor, nil];

And pass this array of sort descriptors to the original method: - (void)sortUsingDescriptors:(NSArray *)sortDescriptors

[myArrayOfLessions sortUsingDescriptors:sortDescriptorArray];

Your original array of lessons is now sorted.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.