0

I've a person object which has NSString properties firstname, lastname, birthday, and NSMutableDictionary of different phone numbers of that person.

I've to add different person objects in an array and sort those objects by their age, I've method to calculate their age but not sure how to link that calculated age with existing array of all person object and sort it.

Please help.

7
  • You know, if you sort by birth date, you sort by age. (Unless some people count dog years, or Jack Benny is one of the people in the list.) Commented Sep 13, 2013 at 21:09
  • On an unrelated note, instance variables should be used to hold state, and their names should reflect that. So I would not use a verb (and the imperative form, at that) as the first part of the addPersonArray variable name. You could for instance call it personArray and the name would reflect what the variable holds. Commented Sep 13, 2013 at 21:09
  • @Monola thank you so much for that correction,I'll do that Commented Sep 13, 2013 at 21:11
  • @HotLicks , birthday in person object is NSString, i can not use it directly to sort array Commented Sep 13, 2013 at 21:12
  • You certainly can use it, with sortedArrayUsingFunction/Selector/Comparator. Commented Sep 13, 2013 at 21:22

3 Answers 3

1

Do I understand you correctly? Do you want to sort your array by the person's age?

Sorting can be rather easy by using the -sortedArrayUsingComparator: method of NSArray. Let's say a person has a property called age which is of type NSInteger. We could sort the array like this:

// lets assume _persons_ is an array of Person objects ...
NSArray *sortedPersons = [persons sortedArrayUsingComparator: ^(Person *p1, Person *p2) {
   if (p1.age > p2.age) {
      return NSOrderedDescending;
   }

   if (p1.age < p2.age) {
       return NSOrderedAscending;
   }

   return NSOrderedSame;
}];

Of course you could do any kind of comparison in the comparator. E.g. you could compare dates, strings, etc... The NSComparisonResult you return will move items inside the array to the correct position.

EDIT

The following might work in your particular situation:

NSArray *sortedPersons = [persons sortedArrayUsingComparator: ^(Person *p1, Person *p2) {
   NSDate *date1 = [p1.birthday asDate];
   NSDate *date2 = [p2.birthday asDate];
   return [date1 compare:date2];
}];
Sign up to request clarification or add additional context in comments.

10 Comments

yes,I've to sort the array by person's age but I don't have perosn's age in person object, I've person's birthday using which I can calculate age, but how can I use this calculated age to sort the array.
You can also sort by date, just change the comparator. See: stackoverflow.com/a/5965106/250164
@czechboy From his code in op, it seems it is an NSString object.
I changed it in accordance with his date parsing in openingspost.
@WolfgangSchreurs so this edit gives me birthdays in NSDate, so when you say return [date1 compare:date2] what happens, i'm in a learning phase, please bear with me. Also what if I have more than 2 objects inside personArry?
|
0

If you want to sort by particular property (such as age), use NSSortDesriptor:

NSArray * people  = ... //your array of Person objects
NSSortDescriptor * sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
NSArray * sortedPeopleByAge = [people sortedArrayUsingDescriptors:@[sortDescriptor];
//sortedPeopleByAge are ... you know ... people sorted by age.

Hope this is what you meant.

1 Comment

age does not exist. He has a birthday which is a string.
0
NSDateFormatter df = [NSDateFormatter new];

persons = [persons sortedArrayUsingComparator: ^(Person *p1, Person *p2) {
   NSDate* d1 = [df dateFromString:p1.birthday];
   NSDate* d2 = [df dateFromString:p2.birthday];

   return [d1 compare:d2];
}];

1 Comment

Of course, the date formatter needs to have it's format set appropriately. Plus, the OP apparently already has a "asDate" category on NSString which returns an NSDate, so the explicit date formatter is unnecessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.