ios - how to convert a dictionary's keys into an array sorted by the values to which those keys correspond in the dictionary -
I have a dictionary; its keys are string and its value is NSDTs, for example:
{A: & lt; John 1 & gt ;, B: & lt; March 1>, C: & lt; Fab 1 & gt; }I want to create an array with keys in the dictionary sorted by date (A, B and C). So I should end up with the following array:
[a, c, b]
How can I do this?
Bonus Points : With that sort array, I can accomplish my ultimate goal, but the sorted arrays will be mediators only; The most elegant solution will not be required. What I really want to do is an array of objects, each of which has a property value related to one of the keys in the dictionary, for example:
[ Obj1, obj2, obj3] where
obj1.prop = a obj2.prop = b obj3.prop = c
I want to sort that array based on date values of A, B, and amp; C refers to the dictionary, therefore, my final goal in this scenario would be the following array:
obj1, obj3, obj2] Thanks!
Building on David's answer, this will solve all this together:
< Code> NSDictionary * items = @ {@ "A": Date1, @ "B": Date 2, @ "C": Date 3}; NSArray * objectsArray = @ {obj1, obj2, obj3}; NSArray * Sorted objectArerere = [ObjectRarra SortedUreusingcomputer: ^ NSComperinaristelt (ID OGJ1, ID OGJ2) {NSDT * Date1 = [Object ObjectForisky: oz 1.prop]; NSDT * Date2 = [Item ObjectForekey: ozji.prop]; Return [compare date1: date2]; }];
Comments
Post a Comment