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





4 comments:

  1. nice! this is very clear!

    ReplyDelete
  2. INTERNATIONAL CONCEPT OF WORK FROM HOME
    Work from home theory is fast gaining popularity because of the freedom and flexibility that comes with it. Since one is not bound by fixed working hours, they can schedule their work at the time when they feel most productive and convenient to them. Women & Men benefit a lot from this concept of work since they can balance their home and work perfectly. People mostly find that in this situation, their productivity is higher and stress levels lower. Those who like isolation and a tranquil work environment also tend to prefer this way of working. Today, with the kind of communication networks available, millions of people worldwide are considering this option.

    Women & Men who want to be independent but cannot afford to leave their responsibilities at home aside will benefit a lot from this concept of work. It makes it easier to maintain a healthy balance between home and work. The family doesn't get neglected and you can get your work done too. You can thus effectively juggle home responsibilities with your career. Working from home is definitely a viable option but it also needs a lot of hard work and discipline. You have to make a time schedule for yourself and stick to it. There will be a time frame of course for any job you take up and you have to fulfill that project within that time frame.

    There are many things that can be done working from home. A few of them is listed below that will give you a general idea about the benefits of this concept.

    Baby-sitting
    This is the most common and highly preferred job that Women & Men like doing. Since in today's competitive world both the parents have to work they need a secure place to leave behind their children who will take care of them and parents can also relax without being worried all the time. In this job you don't require any degree or qualifications. You only have to know how to take care of children. Parents are happy to pay handsome salary and you can also earn a lot without putting too much of an effort.

    Nursery
    For those who have a garden or an open space at your disposal and are also interested in gardening can go for this method of earning money. If given proper time and efforts nursery business can flourish very well and you will earn handsomely. But just as all jobs establishing it will be a bit difficult but the end results are outstanding.

    Freelance
    Freelance can be in different wings. Either you can be a freelance reporter or a freelance photographer. You can also do designing or be in the advertising field doing project on your own. Being independent and working independently will depend on your field of work and the availability of its worth in the market. If you like doing jewellery designing you can do that at home totally independently. You can also work on freelancing as a marketing executive working from home. Wanna know more, email us on workfromhome.otr@gmail.com and we will send you information on how you can actually work as a marketing freelancer.


    Internet related work
    This is a very vast field and here sky is the limit. All you need is a computer and Internet facility. Whatever field you are into work at home is perfect match in the software field. You can match your time according to your convenience and complete whatever projects you get. To learn more about how to work from home, contact us today on workfromhome.otr@gmail.comand our team will get you started on some excellent work from home projects.

    Diet food
    Since now a days Women & Men are more conscious of the food that they eat hence they prefer to have homemade low cal food and if you can start supplying low cal food to various offices then it will be a very good source of income and not too much of efforts. You can hire a few ladies who will help you out and this can be a good business.

    Thus think over this concept and go ahead.

    ReplyDelete
  3. Nice post! Can’t wait for the next one. Keep stuff like this coming.
    Asus Laptops

    ReplyDelete