1.11 A dictionary in the evaluation array

Source: Internet
Author: User

#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. */

Nsmutablearray*newarr3=[nsmutablearray ARRAYWITHARRAY:ARR3];

Nsdictionary*dic=[[nsdictionary alloc]initwithobjectsandkeys:@ "Philip Schiller", @ "name", @ "29", @ "age", @ "female", @ "gender ", @" 70 ", @" score ", nil];

NSLog (@ "Output first question");

NSLog (@ "name%@ age is%@ gender is%@ score is%@", dic[@ "name"],dic[@ "Age"],dic[@ "gender"],dic[@ "score"]);

//nslog (@ "The third question");

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

Nsarray*del=[nsarray arraywithobjects:des, nil];

NSARRAY*SORTARR1=[NEWARR3 Sortusingdescriptors:del];

NSLog (@ "Xing Ming shu Chu");

For (ID str20 in sortarr1) {

NSLog (@ "name%@ age is%@ gender is%@ score is%@", str20[@ "name"],str20[@ "Age"],str20[@ "gender"],str20[@ "score"]);

//

//        }

Second question

Nsmutablearray*arr=[nsmutablearray ARRAYWITHOBJECT:ARR3];

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

if ([arr[i][@ "name"] isequaltostring:@ "Steve Jobs") {

[ARR Removeobject:arr[i]];

}

}

NSLog (@ "after delete");

For (ID STR in ARR) {

NSLog (@ "name is%@ age for%@ sex for%@ score for%@", str[@ "name"],str[@ "ages"],str[@ "Sex"],str[@ "score"]);

}

NSLog (@ "The third question");

Nsarray *arrsort=[newarr3 sortedarrayusingselector: @selector (compare:)];

for (id str in arrsort) {

NSLog (@ "name%@ age is%@ sex is%@ score is%@", str[@ "name"],str[@ "ages"],str[@ "Sex"],str[@ "score"]);

//        }

//

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

Nsarray*del=[nsarray arraywithobjects:des, nil];

NSARRAY*ARRSORT4=[NEWARR3 Sortedarrayusingdescriptors:del];

NSLog (@ "The third question");

For (ID str20 in arrsort4) {

NSLog (@ "name%@ age is%@ sex is%@ score is%@", str20[@ "name"],str20[@ "ages"],str20[@ "Sex"],str20[@ "score"]);

}

NSLog (@ "Fourth question");

Nscomparator comp=^ (ID obj1, id obj2) {

int age1=[obj1[@ "age"]intvalue];

int age2=[obj2[@ "age"]intvalue];

if (age1==age2) {

return [obj1[@ "Sex"] compare:obj2[@ "sex"];

}

if (age1<age2) {

return nsorderedascending;

}else if (age1>age2) {

return nsordereddescending;

}else{

return nsorderedsame;

}

};

NSARRAY*ARRSORT2=[NEWARR3 sortedarrayusingselector: @selector (compare:)];

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

Nsarray*del1=[nsarray arraywithobjects:des1, nil];

NSARRAY*ARRSORT5=[NEWARR3 Sortedarrayusingdescriptors:del1];

For (ID one in arrsort5) {

NSLog (@ "name%@ age is%@ sex is%@ score is%@", one[@ "name"],one[@ "ages"],one[@ "Sex"],one[@ "score"]);

}

NSLog (@ "fifth question");

For (ID of NEWARR3) {

if ([both [@ "score"] intvalue]>=80) {

NSLog (@ "name%@ age is%@ sex is%@ score is%@", two[@ "name"],two[@ "ages"],two[@ "Sex"],two[@ "score"]);

}

}

}

return 0;

}

1.11 A dictionary in the evaluation array

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.