ios - How to properly sort an NSDictonary by key values that contain strings of time? -
i'm not understanding why it's not sorting properly. have nsdictionary contains key values of times in day. when try sort it, it's not ordered.
events = @{@"12:00 pm" : @[@"1"], @"12:30 pm" : @[@"2"], @"1:00 pm" : @[@"3", @"4"], @"1:30 pm" : @[@"5", @"11"], @"2:00 pm" : @[@"6"], @"3:00 pm" : @[@"7"], @"3:30 pm" : @[@"8"], @"4:00 pm" : @[@"9"], @"4:30 pm" : @[@"10", @"11"], @"5:00 pm" : @[@"12", @"13", @"14"]}; eventtimes = [ [events allkeys] sortedarrayusingselector:@selector(localizedcaseinsensitivecompare:)];
when nslog eventtimes, produces output:
( "1:00 pm", "1:30 pm", "12:00 pm", "12:30 pm", "2:00 pm", "3:00 pm", "3:30 pm", "4:00 pm", "4:30 pm", "5:00 pm" )
how can achieve output?
( "12:00 pm", "12:30 pm", "1:00 pm", "1:30 pm", "2:00 pm", "3:00 pm", "3:30 pm", "4:00 pm", "4:30 pm", "5:00 pm" )
update: tried:
nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setlocale:[[nslocale alloc] initwithlocaleidentifier:@"en_us"] ]; [formatter setdateformat:@"hh:mm"]; nsstring *etastr = [eventtimes objectatindex:section]; nsdate *generateddate = [formatter datefromstring:etastr]; nslog(@"%@", [formatter stringfromdate:generateddate]);
however, nil.
- keep time number of seconds, timestamp or
nsdate
. - use regular sort.
- use
nsdateformatter
produce time string in preferred format. keep application data in format friendly internal data organization , formatting before display on screen.
regarding nsdateformatter
questions , how convert time string nsdate
, played around in playground , seems key set shortstyle
format time, setup en_us
locale since convert 12h time , make sure use utc
timezone avoid time offset because time absolute anyway.
// playground - noun: place people can play import uikit // input var times = [ "3:30 pm", "4:00 pm", "4:30 pm", "5:00 pm", "12:30 pm", "12:00 pm", "1:00 pm", "1:30 pm", "2:00 pm", "3:00 pm" ] // create array of nsdate objects var dates = nsmutablearray() println("convert time string nsdate") println() // setup formatter time style let formatter = nsdateformatter() formatter.dateformat = "h:mm a" formatter.datestyle = nsdateformatterstyle.nostyle formatter.timestyle = nsdateformatterstyle.shortstyle formatter.timezone = nstimezone(name: "utc") formatter.locale = nslocale(localeidentifier: "en_us") // loop thru input data , convert time string nsdate t in times { let date = formatter.datefromstring(t) dates.addobject(date!) println("t = \(t) converted \(date)") } println() println("sort array of nsdate objects") println() // sort dates let sorteddates = dates.sortedarrayusingdescriptors([ nssortdescriptor(key: "self", ascending: true) ]) // print out sorted array of dates d in sorteddates { let date = d nsdate let formattedstring = formatter.stringfromdate(date) println("\(formattedstring)") }
now of in swift sure it's intuitive enough write same code in objective-c.
the console output:
convert time string nsdate t = 3:30 pm converted optional(2000-01-01 15:30:00 +0000) t = 4:00 pm converted optional(2000-01-01 16:00:00 +0000) t = 4:30 pm converted optional(2000-01-01 16:30:00 +0000) t = 5:00 pm converted optional(2000-01-01 17:00:00 +0000) t = 12:30 pm converted optional(2000-01-01 12:30:00 +0000) t = 12:00 pm converted optional(2000-01-01 12:00:00 +0000) t = 1:00 pm converted optional(2000-01-01 13:00:00 +0000) t = 1:30 pm converted optional(2000-01-01 13:30:00 +0000) t = 2:00 pm converted optional(2000-01-01 14:00:00 +0000) t = 3:00 pm converted optional(2000-01-01 15:00:00 +0000) sort array of nsdate objects 12:00 pm 12:30 pm 1:00 pm 1:30 pm 2:00 pm 3:00 pm 3:30 pm 4:00 pm 4:30 pm 5:00 pm
Comments
Post a Comment