Dictionary add a category, delete, sort by name, sort by age

Source: Internet
Author: User

#define NSLOG (FORMAT, ...) fprintf (stderr, "%s\n", [[NSString Stringwithformat:format, # #__VA_ARGS__] utf8string]);

#import <Foundation/Foundation.h>

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

@autoreleasepool {

Nsarray *[email protected][@{@ "name": @ "Tim Cook", @ "age": @ "@", @ "sex": @ "female", @ "score": @ "},@{@", "name": @ "Jony Ive ", @" age ": @" + "@" Sex ": @" female ", @" score ": @"},@{@ "," name ": @" Steve Jobs "@" Age ": @" @ ", @" sex ": @" male ", @" score " : @ "},@{@" "name": @ "Robert Brunne", @ "age": @ "@" @ "Sex": @ "male" @ "score": @ "88"}];

/*

1. Add data name: Philip Schiller Age: 29 Sex: Female score: 70 within the ARR array.

2. Find the data for "Steve Jobs" in the array and delete it.

3. Sort by first letter of name.

4. Sort ascending by age, or by gender if the age is the same.

5. Students who have output scores greater than or equal to 80 points. */

Nsdictionary *[email protected]{@ "name": @ "Philip Schiller",

@ "Age": @ "29",

@ "Sex": @ "female",

@ "score": @ "70"};

Nsmutablearray *arr3=[nsmutablearray Arraywitharray:arr];

[Arr3 Addobject:dic];

For (Nsdictionary *dic1 in ARR3)

{

NSLog (@ "Name:%@, Age:%@, Gender:%@, Score:%@", dic1[@ "name"],dic1[@ "ages"],dic1[@ "Sex"],dic1[@ "score"]);

}

NSLog (@ "\ n");

2. Find the data of "Steve Jobs" in the array and delete it.

Nsmutablearray *arr4=[nsmutablearray Array];

for (int i=0;i<arr3.count;i++)

{

Nsmutabledictionary *dic2=arr3[i];

if (![ dic2[@ "name"] isequalto:@ "Steve Jobs")

{

[Arr4 Addobject:dic2];

}

}

For (Nsdictionary *dic3 in ARR4)

{

NSLog (@ "Name:%@, Age:%@, Gender:%@, Score:%@", dic3[@ "name"],dic3[@ "ages"],dic3[@ "Sex"],dic3[@ "score"]);

}

NSLog (@ "\ n");

3. Sort by first letter of name.

Nssortdescriptor *sortwithname=[[nssortdescriptor alloc]initwithkey:@ "name" Ascending:yes];

Nsarray *elesort=[nsarray Arraywithobjects:sortwithname,nil];

Nsarray *SORTARR=[ARR4 Sortedarrayusingdescriptors:elesort];

For (Nsdictionary *dic4 in Sortarr) {

NSLog (@ "Name:%@, Age:%@, Gender:%@, Score:%@", dic4[@ "name"],dic4[@ "ages"],dic4[@ "Sex"],dic4[@ "score"]);

}

NSLog (@ "\ n");

4. Sort ascending by age, or by gender if the age is the same.

Nssortdescriptor *sortwithage=[[nssortdescriptor alloc]initwithkey:@ "age" ascending:yes];

Nssortdescriptor *sortwithsex=[[nssortdescriptor alloc]initwithkey:@ "Sex" ascending:yes];

Nsarray *elesorts=[nsarray Arraywithobjects:sortwithage,sortwithsex,nil];

Nsarray *SORTARRS=[ARR4 Sortedarrayusingdescriptors:elesorts];

For (Nsdictionary *dic4 in Sortarrs) {

NSLog (@ "Name:%@, Age:%@, Gender:%@, Score:%@", dic4[@ "name"],dic4[@ "ages"],dic4[@ "Sex"],dic4[@ "score"]);

}

NSLog (@ "\ n");

5. Students who have output scores greater than or equal to 80 points.

Nsmutablearray *m=[nsmutablearray Array];

for (int i=0;i<arr.count;i++)

{

Nsdictionary *dic6=arr[i];

Nsinteger n=[dic6[@ "Score"] intvalue];

if (n>=80)

{

[M ADDOBJECT:DIC6];

}

}

For (Nsdictionary *dic7 in M) {

NSLog (@ "Name:%@, Age:%@, Gender:%@, Score:%@", dic7[@ "name"],dic7[@ "ages"],dic7[@ "Sex"],dic7[@ "score"]);

}

}

return 0;

}

Dictionary add a category, delete, sort by name, sort by age

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.