Ios_ dictionary arrays grouped and sorted by key

Source: Internet
Author: User

main.m//sortgroup////Created by Beyond in 14-10-26.//Copyright (c) 2014 Beyond.com All rights reserved.//Press G Rouptag grouping and Sort by order field enabled means not enabling #import <foundation/foundation.h>int main (int argc, const char * argv[]) {@aut                                          Oreleasepool {//1. Define a dictionary array of tests Nsmutablearray *dictarray = @[@{ @ "fenabled": [NSNumber numberwithint:1], @ "F                                          Grouptag ": [NSNumber numberwithint:0], @" Forder ": [NSNumber numberwithint:0],                                      @ "FName": @ "Bao Yu"},                                          @{@ "fenabled": [NSNumber numberwithint:1], @ "Fgrouptag": [NSNumber numberwithint:1], @ "Forder": [Nsnu  Mber numberwithint:0],                                        @ "FName": @ "Daiyu"},                                          @{@ "fenabled": [NSNumber numberwithint:0], @ "Fgrouptag": [NSNumber numberwithint:1], @ "Forder": [Nsnu                                          Mber numberwithint:1], @ "FName": @ "Bao Chai" }, @{@ "fenabled": [NSNumber Numberwithint:                                          1], @ "Fgrouptag": [NSNumber numberwithint:1],                                          @ "Forder": [NSNumber Numberwithint:2], @ "FName": @ "Xiangyun" }, @{@ "fenabled": [NSNumber Numberwithint:1], @ "Fgrouptag": [NSNumber Numberwithint:2],                                          @ "Forder": [NSNumber numberwithint:0], @ "FName": @ "Miaoyu" }, @{@ "fenabled":                                          [NSNumber numberwithint:1], @ "Fgrouptag": [NSNumber Numberwithint:3],  @ "Forder": [NSNumber numberwithint:0], @ "FName":                                          @ "Ching Wen"}, @{ @ "fenabled": [NSNumber numberwithint:1], @ "Fgrouptag": [NSNumber Numb                                          Erwithint:3], @ "Forder": [NSNumber numberwithint:1], @ "FName": @ "Hit People"}        ]; Nsarray to Nsmutablearray//0, for non-enabled, the dictionary model with enabled 0, delete nsmutablearray *dictarr = [Nsmutablearray array        ];            for (int i = 0; i < Dictarray.count; i++) {nsdictionary *dict = dictarray[i];            if ([[Dict objectforkey:@ "fenabled"]intvalue] = = 1) {[Dictarr addobject:dict];                }}//NSLog (@ "Clears the array after the dictionary is not enabled:%@", Dictarr); 1. Sort the array by grouptag nsarray *sortdesc = [Nsarray arraywithobject:[nssortdescriptor sortdescriptorwithkey:@ "FGroupTa        G "Ascending:yes]";        Nsarray *sortedarr = [Dictarr Sortedarrayusingdescriptors:sortdesc];                        NSLog (@ "sorted array:%@", Sortedarr);        2, the array is grouped, press Grouptag//traversal, create a set of arrays, the array of each element is a model array nsmutablearray *_grouparr = [Nsmutablearray array]; Nsmutablearray *currentarr = [Nsmutablearray Array];        NSLog (@ "class--%@", [Currentarr class]);        Because there must be a dictionary to return, first add a [Currentarr addobject:sortedarr[0]];        [_grouparr Addobject:currentarr];                If more than one, the animation is added if (Sortedarr.count > 1) {for (int i = 1; i < Sortedarr.count; i++) { The first dictionary model of the previous model array in the group array is first removed groupid nsmutablearray *premodelarr = [_grouparr Objectatindex:_grouparr.cou                NT-1];                int pregroupid = [[[Premodelarr objectatindex:0] objectforkey:@ "Fgrouptag"] intvalue];                Takes out the current dictionary, and, based on groupid comparisons, adds to the same model array if the same is not the same group nsdictionary *currentdict = Sortedarr[i];                int GroupID = [[Currentdict objectforkey:@ "Fgrouptag"] intvalue];                if (GroupID = = pregroupid) {[Currentarr addobject:currentdict]; }else{//If not the same, the description has a new set, then create a model array and add to the group array _grouparr currentarr = [Nsmutablearray ar                    Ray]; [CURrentarr Addobject:currentdict];                [_grouparr Addobject:currentarr];        }}}//3, traverse to sort each group nsmutablearray *tempgrouparr = [Nsmutablearray array]; For (Nsmutablearray *arr in _grouparr) {Nsarray *sortdesc = [Nsarray arraywithobject:[nssortdescriptor sortDes            criptorwithkey:@ "Forder" ascending:yes];  Nsmutablearray *temparr = [arr sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {if ([[Obj1 objectforkey:@ "Forder"]intvalue "< [[Obj2 objectforkey:@" Forder "]intvalue]) {return nsorderedascend                ing                    if ([[Obj1 objectforkey:@] Forder "]intvalue" > [[Obj2 objectforkey:@ "Forder"]intvalue]) {                return nsordereddescending;            } return nsorderedsame;            }];        [Tempgrouparr Addobject:temparr];                } _grouparr = Tempgrouparr; NSLog (@ "packaged GroupArray:%@ ", _grouparr); } return 0;}

Ios_ dictionary arrays grouped and sorted by key

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.