IOS _ dictionary array groups and sorts by key, ioskey

Source: Internet
Author: User
Tags sorts

IOS _ dictionary array groups and sorts by key, ioskey

//// Main. m // SortGroup /// Created by beyond on 14-10-26. // Copyright (c) 2014 beyond.com All rights reserved. // enabled is disabled when grouping by GroupTag and sorting by Order field # import <Foundation/Foundation. h> int main (int argc, const char * argv []) {@ autoreleasepool {// 1. define a test dictionary array NSMutableArray * dictArray = @ [@ {@ "FEnabled": [NSNumber numberWithInt: 1], @ "FGroupTag": [NSNumber numberWithInt: 0], @ "FOrder": [NSNumber numberWithInt: 0], @ "FName": @ "Baoyu"}, @ {@ "FEnabled": [NSNumber numberWithInt: 1], @ "FGroupTag": [NSNumber numberWithInt: 1], @ "FOrder": [NSNumber numberWithInt: 0], @ "FName": @ ""}, @ {@ "FEnabled": [NSNumber numberWithInt: 0], @ "FGroupTag": [NSNumber numberWithInt: 1], @ "FOrder": [NSNumber numberWithInt: 1], @ "FName": @ ""}, @ {@ "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": @" "}, @ {@ "FEnabled": [NSNumber numberWithInt: 1], @ "FGroupTag": [NSNumber numberWithInt: 3], @ "FOrder": [NSNumber numberWithInt: 0], @ "FName": @ ""}, @ {@ "FEnabled": [NSNumber numberWithInt: 1], @ "FGroupTag": [NSNumber numberWithInt: 3], @ "FOrder": [NSNumber numberWithInt: 1], @ "FName": @ "attacker"}]; // NSArray is converted to NSMutableArray // 0. If not enabled, that is, if the value of enabled is 0, the 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 (@ "Clear the array after the disabled dictionary: % @", dictArr ); // 1. Sort arrays by GroupTag NSArray * sortDesc = [NSArray arrayWithObject: [NSSortDescriptor attributes: @ "FGroupTag" ascending: YES]; NSArray * sortedArr = [dictArr attributes: sortDesc]; // NSLog (@ "sorted array: % @", sortedArr); // 2. group the array by GroupTag //, and create a group array, each element in the group array is a model array NSMutableArray * _ groupArr = [NSMutableArray array]; NSMutableArray * currentArr = [NSMutableArray array]; NSLog (@ "class -- % @", [currentArr class]); // because there must be a dictionary returned, add a [currentArr addObject: sortedArr [0]; [_ groupArr addObject: currentArr]; // if there are more than one, add if (sortedArr. count> 1) {for (int I = 1; I <sortedArr. count; I ++) {// retrieve the groupID NSMutableArray * preModelArr = [_ groupArr objectAtIndex: _ groupArr OF THE FIRST dictionary model in the previous model array of the group. count-1]; int preGroupID = [[[preModelArr objectAtIndex: 0] objectForKey: @ "FGroupTag"] intValue]; // retrieve the current dictionary and compare it with the groupID, if they are the same, they are added to the same model array. If they are not the same, NSDictionary * currentDict = sortedArr [I]; int groupID = [[currentDict objectForKey: @ "FGroupTag"] intValue]; if (groupID = preGroupID) {[currentArr addObject: currentDict];} else {// if not the same, there is a new group, create a model array and add it to the group array _ groupArr currentArr = [NSMutableArray array]; [currentArr addObject: currentDict]; [_ groupArr addObject: currentArr] ;}}} // 3. Sort each group by traversal. NSMutableArray * tempGroupArr = [NSMutableArray array]; for (optional * arr in _ groupArr) {NSArray * sortDesc = [NSArray arrayWithObject: [NSSortDescriptor Syntax: @ "FOrder" ascending: YES]; NSMutableArray * tempArr = [arr Syntax: ^ NSComparisonResult (id obj1, id obj2) {if ([obj1 objectForKey: @ "FOrder"] intValue] <[[obj2 objectForKey: @ "FOrder"] intValue]) {return NSOrderedAscending;} if ([[obj1 objectForKey: @ "FOrder"] intValue]> [[obj2 objectForKey: @ "FOrder"] intValue]) {return NSOrderedDescending;} return NSOrderedSame;}]; [tempGroupArr addObject: tempArr];} _ groupArr = tempGroupArr; NSLog (@ "encapsulated group array: % @", _ groupArr);} return 0 ;}


VC ++ two-dimensional array dictionary ordered sorting

Change scanf ("% s,", name [I]); To gets (name [I.
When scanf is used to input a string, leading to the end of the string when it encounters a space, the character following the space is entered into the next name;
Gets () is considered to end with a string input only after the carriage return.

Write a program and enter the names of several students. Use a pointer array to point to this set of strings. Sort these strings alphabetically and Output

Hope to help you.
# Include <iostream> # include <string. h> using namespace std; const int SIZE = 256; char * pName [SIZE]; int main () {int I = 0, n = 5; while (I <n) {char tmp [32] = {0}; cin> tmp; pName [I] = new char [strlen (tmp) + 1]; strcpy (pName [I], tmp); pName [I] [strlen (tmp)] = '\ 0'; I ++;} char * p = NULL; for (int p1 = 0; p1 <n; p1 ++) {for (int pos = n-1; pos> p1; pos --) {if (strcmp (pName [pos-1], pName [pos])> 0) {p = pName [pos]; pName [pos] = pName [pos-1]; pName [pos-1] = p ;}}for (int p1 = 0; p1 <n; p1 ++) {cout <pName [p1] <endl ;}}
 

Related Article

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.