Showing posts with label NSSortDescriptor NSDate. Show all posts
Showing posts with label NSSortDescriptor NSDate. Show all posts

Tuesday, August 23, 2011

NSSortDescriptor Example

You  have a class Human with following structure:

@interface HumanClass : NSObject {
    
    NSString *name;
    NSString *lastname;
    NSInteger age;
    NSString * city;
    NSDate *birthDate;
    
}



Our array with objects of HumanClass looks like following


Reetu  raj ,45,  NY,  01.05.2011|15:46:40
Mike  Hemington ,15,  New Delhi,  01.05.2011|16:05:00
pinku  Ji ,25,  chandigarh,  01.05.2011|16:20:00
anita  singh ,35,  ramgarh,  09.04.2011|16:00:00
Ankita  Singh ,55,  barbados,  01.05.2011|16:03:20
Ram  Malhotra ,27,  New York,  02.05.2011|19:33:20
Sita  Raman ,34,  London,  01.05.2011|18:33:20


__________________________________________________________________________________________


Lets sort it by date by applying a date sort descriptor. Note that we have given the key  birthDate(HumanClasss's member var string)

        NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"birthDate" ascending:YES selector:@selector(compare:)];
    [humanArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
    [self printHumanArray];




You can change ascending  to NO to make it descending .Result looks like following:


anita  singh ,35,  ramgarh,  09.04.2011|16:00:00
Reetu  raj ,45,  NY,  01.05.2011|15:46:40
Ankita  Singh ,55,  barbados,  01.05.2011|16:03:20
Mike  Hemington ,15,  New Delhi,  01.05.2011|16:05:00
pinku  Ji ,25,  chandigarh,  01.05.2011|16:20:00
Sita  Raman ,34,  London,  01.05.2011|18:33:20
Ram  Malhotra ,27,  New York,  02.05.2011|19:33:20


__________________________________________________________________________________________


       sortDesc = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(compare:)];
    [humanArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
    [self printHumanArray];







Our result looks like following with array sorted by name but sorting is case-sensitive.


Ankita  Singh ,55,  barbados,  01.05.2011|16:03:20
Mike  Hemington ,15,  New Delhi,  01.05.2011|16:05:00
Ram  Malhotra ,27,  New York,  02.05.2011|19:33:20
Reetu  raj ,45,  NY,  01.05.2011|15:46:40
Sita  Raman ,34,  London,  01.05.2011|18:33:20
anita  singh ,35,  ramgarh,  09.04.2011|16:00:00
pinku  Ji ,25,  chandigarh,  01.05.2011|16:20:00


__________________________________________________________________________________________


        sortDesc = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    [humanArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
    [self printHumanArray];




We created case-insensitive sorting descriptor for name and our result looks like following:


anita  singh ,35,  ramgarh,  09.04.2011|16:00:00
Ankita  Singh ,55,  barbados,  01.05.2011|16:03:20
Mike  Hemington ,15,  New Delhi,  01.05.2011|16:05:00
pinku  Ji ,25,  chandigarh,  01.05.2011|16:20:00
Ram  Malhotra ,27,  New York,  02.05.2011|19:33:20
Reetu  raj ,45,  NY,  01.05.2011|15:46:40
Sita  Raman ,34,  London,  01.05.2011|18:33:20


__________________________________________________________________________________________

We want to sort by first name as well as last name but by general convention it should be localized and case-insensitive. So our descriptor looks like following


      sortDesc = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    NSSortDescriptor *sortDesc1 = [[NSSortDescriptor alloc] initWithKey:@"lastname" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    [humanArray sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc,sortDesc1, nil]];
    [self printHumanArray];







And our result looks like following


anita  singh ,35,  ramgarh,  09.04.2011|16:00:00
Ankita  Singh ,55,  barbados,  01.05.2011|16:03:20
Mike  Hemington ,15,  New Delhi,  01.05.2011|16:05:00
pinku  Ji ,25,  chandigarh,  01.05.2011|16:20:00
Ram  Malhotra ,27,  New York,  02.05.2011|19:33:20
Reetu  raj ,45,  NY,  01.05.2011|15:46:40
Sita  Raman ,34,  London,  01.05.2011|18:33:20


__________________________________________________________________________________________

We want to sort by multiple keys, first by city followed by name, so our descriptor looks like following:


       sortDesc = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    sortDesc1 = [[NSSortDescriptor alloc] initWithKey:@"city" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    [humanArray sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc1,sortDesc, nil]];
    [self printHumanArray1];




And our result looks like following. Note I am using another method(printHumanArray1) to print the values in this code snippet.


barbados, Ankita  Singh ,55, 01.05.2011|16:03:20
chandigarh, pinku  Ji ,25, 01.05.2011|16:20:00
London, Sita  Raman ,34, 01.05.2011|18:33:20
New Delhi, Mike  Hemington ,15, 01.05.2011|16:05:00
New York, Ram  Malhotra ,27, 02.05.2011|19:33:20
NY, Reetu  raj ,45, 01.05.2011|15:46:40
ramgarh, anita  singh ,35, 09.04.2011|16:00:00






HERE IS SAMPLE CODE DOWNLOAD LINK