Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-27-2011, 03:21 PM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 33
smithms is on a distinguished road
Smile NSSortDescriptor sort dictionary strings as dates

I have an array of dictionaries "_displayedObjects" where one of the keys "receiptDate" contains the string representation of date values like "12/23/11", "3/5/11", etc.

I need to sort "_displayedObjects" on the key "receiptDate" by the actual Date but still return a sorted array of dictionaries where the "receiptDate" key values remain as strings.

The code below sorts the "receiptDate" key as strings. How would I sort this by Date? Would I create a copy of the array where I have converted all of the "receiptDate" key values to NSDate values, sort it by Date and then convert the values back to strings? Not exactly sure how to approach this.

Code:
NSSortDescriptor * sortDesc1 = [[NSSortDescriptor alloc] initWithKey:(@"receiptDate") ascending:YES];
NSSortDescriptor * sortDesc2 = [[NSSortDescriptor alloc] initWithKey:(@"title") ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];         
		
[_displayedObjects sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc1, sortDesc2, nil]];
[self setTitle:@"Sorted by Date"];
Thanks in advance
smithms is offline   Reply With Quote
Old 12-27-2011, 04:40 PM   #2 (permalink)
Use [code] tags please
 
Join Date: Jun 2009
Location: Jacksonville, FL
Posts: 410
timle8n1 is on a distinguished road
Default

On iOS 4+ you can use initWithKey:ascending:comparator: on NSSortDescriptor.


Code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"mm/dd/yy"];

NSComparator compareDates = ^(id string1, id string2) {
  NSDate *date1 = [formatter dateFromString:string1];
  NSDate *date2 = [formatter dateFromString:string2];
        
  return [date1 compare:date2];
};

NSSortDescriptor * sortDesc1 = [[NSSortDescriptor alloc] initWithKey:@"receiptDate" ascending:YES comparator:compareDates];
NSSortDescriptor * sortDesc2 = [[NSSortDescriptor alloc] initWithKey:(@"title") ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];         
		
[_displayedObjects sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc1, sortDesc2, nil]];
[self setTitle:@"Sorted by Date"];
timle8n1 is offline   Reply With Quote
Old 12-28-2011, 12:45 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 33
smithms is on a distinguished road
Default

Brilliant!... that worked perfectly for me. Thank you so much for your help.
smithms is offline   Reply With Quote
Reply

Bookmarks

Tags
array, date converter, dictionary, nsdate, nssortdescriptor

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 387
13 members and 374 guests
7twenty7, AppsBlogger, Creativ, Dalia, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:51 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0