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.

user2769614
  • 247
  • 3
  • 6
  • 23
  • 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.) – Hot Licks Sep 13 '13 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. – Monolo Sep 13 '13 at 21:09
  • @Monola thank you so much for that correction,I'll do that – user2769614 Sep 13 '13 at 21:11
  • @HotLicks , birthday in person object is NSString, i can not use it directly to sort array – user2769614 Sep 13 '13 at 21:12
  • You certainly can use it, with sortedArrayUsingFunction/Selector/Comparator. – Hot Licks Sep 13 '13 at 21:22
  • I've already tried sorting it by birthday it does not sort the array correct because birthday is NSString – user2769614 Sep 13 '13 at 22:26
  • You haven't been reading the answers, have you? Wolfgang's second scheme will do it just fine. – Hot Licks Sep 14 '13 at 23:05

3 Answers3

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];
}];
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • 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. – user2769614 Sep 13 '13 at 21:06
  • You can also sort by date, just change the comparator. See: http://stackoverflow.com/a/5965106/250164 – Wolfgang Schreurs Sep 13 '13 at 21:10
  • @czechboy From his code in op, it seems it is an `NSString` object. – Léo Natan Sep 13 '13 at 21:12
  • I changed it in accordance with his date parsing in openingspost. – Wolfgang Schreurs Sep 13 '13 at 21:18
  • @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? – user2769614 Sep 13 '13 at 21:44
  • I guess it's obvious, but which date is returned smaller or greater? – user2769614 Sep 13 '13 at 21:55
  • That is the $64 question. Even when you read the spec you're not entirely sure. – Hot Licks Sep 13 '13 at 21:59
  • @user2769614 Apple hides the sorting implementation from you using the comparator method, but I think it basically uses something like the bubblesort algorithm to sort items in the array. The comparison is run for every item in the array, so it will work regardless of how many objects the array has. [Read about bubblesort here](http://nl.wikipedia.org/wiki/Bubblesort). As for what the compare function does, you should read up on the NSDate docs, but it will basically return a value indicating 1 of 3 situations: date 1 is either _greater as_(+1), _smaller as_(-1) or _equal to_(0) date 2. – Wolfgang Schreurs Sep 13 '13 at 22:36
  • when checked I can see first person's birthday in date1 and second person's bithday in date2, but I've one more person object in personArray – user2769614 Sep 13 '13 at 22:44
  • @user2769614 I guess it's still a bit unclear for you how the comparator function works. What it does, is run through all object in the array. It will compare 2 objects every time and change the position if needed. Then it will compare the last object with the next object. Then again the last object with the next object. Put some logging in the comparator function to see what happens. – Wolfgang Schreurs Sep 14 '13 at 11:40
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.

czechboy
  • 899
  • 7
  • 15
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];
}];
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • 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. – Hot Licks Sep 13 '13 at 21:25