ios - Sorting NSArray of NSDictionary...not completely working -


so have nsmutablearray of nsdictionary items. each nsdictionary item has key "name" should used sort array alphabetically. except "sorting" partially works. not sorting in exact alphabetical order. it's moving of dictionary in order, leaves out of order.

nsmutablearray *section1 = [champs lastobject]; nsarray *oldsection1 = [nsarray arraywitharray:section1]; [section1 sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {     nsdictionary *dicta = (nsdictionary *)obj1;     nsdictionary *dictb = (nsdictionary *)obj2;      nsstring *champname1 = dicta[@"name"];     nsstring *champname2 = dictb[@"name"];     return [champname1 compare:champname2]; }]; // animation (nsdictionary *champinfo2 in section1) {     if ([oldsection1 indexofobject:champinfo2] != [section1 indexofobject:champinfo2]) {         [self.collectionview moveitematindexpath:[nsindexpath indexpathforitem:[oldsection1 indexofobject:champinfo2] insection:1] toindexpath:[nsindexpath indexpathforitem:[section1 indexofobject:champinfo2] insection:1]];     } } 

i tried code

nsmutablearray *section1 = [champs lastobject]; nsarray *oldsection1 = [nsarray arraywitharray:section1]; /*[section1 sortusingcomparator:^nscomparisonresult(id obj1, id obj2) {     nsdictionary *dicta = (nsdictionary *)obj1;     nsdictionary *dictb = (nsdictionary *)obj2;      nsstring *champname1 = dicta[@"name"];     nsstring *champname2 = dictb[@"name"];     return [champname1 compare:champname2]; }];*/ section1 = [[nsarray arraywitharray:section1] sortedarrayusingdescriptors:[nsarray arraywithobject:[nssortdescriptor sortdescriptorwithkey:@"name" ascending:yes]]].mutablecopy; // animation (nsdictionary *champinfo2 in section1) {     if ([oldsection1 indexofobject:champinfo2] != [section1 indexofobject:champinfo2]) {         [self.collectionview moveitematindexpath:[nsindexpath indexpathforitem:[oldsection1 indexofobject:champinfo2] insection:1] toindexpath:[nsindexpath indexpathforitem:[section1 indexofobject:champinfo2] insection:1]];     } } 

ok problem wasn't updating snapshot of content before reorganization updated visually. updated code:

// animation                 (nsdictionary *champinfo2 in section) {                     if ([oldsection indexofobject:champinfo2] != [section indexofobject:champinfo2]) {                         [self.collectionview moveitematindexpath:[nsindexpath indexpathforitem:[oldsection indexofobject:champinfo2] insection:[champs indexofobject:section]] toindexpath:[nsindexpath indexpathforitem:[section indexofobject:champinfo2] insection:[champs indexofobject:section]]];                         [oldsection removeobject:champinfo2];                         [oldsection insertobject:champinfo2 atindex:[section indexofobject:champinfo2]];                     }                 } 

the upper part of code (i.e. sorting) works fine. can simplified if use sort descriptors, it's not issue.

the real problem code moves things around.

before first move indexing of oldsection1 , self.collectionview in sync, i.e. if item xyz @ index i in oldsection1, @ index i in self.collectionview.

after first move, however, indexing gets out of sync, because move executed in self.collectionview, not in oldsection1. example, if start

oldsection1    = c d b collectionview = c d b 

and want be

sorted =         b c d 

after first move of c data going like

oldsection1    = c d b collectionview = c d b 

when it's time move d new spot, c gets moved instead.

to fix issue, make oldsection1 mutable, , execute "parallel" moves move things around in self.collectionview.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -