ios - How to sort NSArray items into groups based on the same object order in a different NSArray? -


i have nsarray of unique uuids sorted in proper order. have nsarray of bookmarks uuid encoding. sort nsarray of bookmarks based on uuid property groups.

for example, have nsarray: @[@"uuid1", @"uuid3", @"uuid2"] has been sorted particular order. other nsarray must sort of bookmarks in same order first nsarray above.

so second nsarray is: @[@"bookmark1", @"bookmark2", @"bookmark3", ...etc.]

say bookmark 1 has uuid property encoded uuid2, bookmark 2 has uuid encoding of uuid 1, bookmark 3 has encoding of uuid3. how can sort , group these bookmarks be: @[@"bookmark2", @"bookmark3", @"bookmark1"]?

thanks!

you rid of second array , use dictionary instead, keyed on uuid.

nsarray *sortedids = @[ @"uuid1", @"uuid3", @"uuid2", ];  nsdictionary *items = @{   @"uuid1" : @[ bookmark1 ],   @"uuid2" : @[ bookmark2 ],   @"uuid3" : @[ bookmark3 ], }; 

now when want second bookmark can access with

nsarray *bookmarksforuuid = items[sortedids[1]]; 

if wanted build structure above add category below nsarray

- (nsdictionary *)pas_groupby:(id (^)(id object))block; {   nsparameterassert(block);    nsmutabledictionary *groupeddictionary = nsmutabledictionary.new;    (id object in self) {     id key = block(object);     if (groupeddictionary[key]) {       [groupeddictionary[key] addobject:object];     } else {       groupeddictionary[key] = [nsmutablearray arraywithobject:object];     }   }    return groupeddictionary; } 

then assuming bookmark objects like

@interface bookmark : nsobject  @property (nonatomic, copy) nsstring *uuid; // other properties // other properties  @end 

you can use category this

nsdictionary *bookmarksmappedbysection = ({   return [bookmarks pas_groupby:^(bookmark *bookmark) {     return bookmark.uuid;   }; }); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -