我需要列出所有字典值。像最高分到最低分。我怎么处理这个?
我需要在表格视图中列出所有标记和相应的名称和详细信息。
2015-04-06 14:48:53.381 camelProject[3310:597950] valueforkey title = (
{
CamelID = 237;
Color = bg;
Comments = "Fhjfnfnfjfihgjfmsndidm,almcmcuirowmvn vmmc Jdmdmcm";
CompID = 235;
DateOfBirth = "/Date(1424725200000)/";
Description = "rtrtttrtrehndskvn;lkdf;lm;mgemln;lm';' jknt;l";
FromUserID = 564987631;
FromUsername = 564987631;
ID = 4;
IndvMarks = "5;5;3;4;2;2;2;2;5;9";
Marks = 39;
Name = name3sv;
OutOF = 100;
ProfilePhoto = "2D0B36F3-71B3-4B5F-A375-7AD4D0AFF1AF.jpg\n";
TOname = hasnam;
ToUserName = 564987631;
Types = dsfv;
UserID = 564987631;
fromname = hasnam;
},
{
CamelID = 237;
Color = bg;
Comments = "";
CompID = 235;
DateOfBirth = "/Date(1424725200000)/";
Description = "rtrtttrtrehndskvn;lkdf;lm;mgemln;lm';' jknt;l";
FromUserID = 564987631;
FromUsername = 564987631;
ID = 5;
IndvMarks = "5;4;5;7;6;0;6;5;4;6";
Marks = 48;
Name = name3sv;
OutOF = 100;
ProfilePhoto = "2D0B36F3-71B3-4B5F-A375-7AD4D0AFF1AF.jpg\n";
TOname = hasnam;
ToUserName = 564987631;
Types = dsfv;
UserID = 564987631;
fromname = hasnam;
},
{
CamelID = 237;
Color = bg;
Comments = Gucjhkhvnkvnvnvknkjbkvjhvvkb;
CompID = 235;
DateOfBirth = "/Date(1424725200000)/";
Description = "rtrtttrtrehndskvn;lkdf;lm;mgemln;lm';' jknt;l";
FromUserID = 564987631;
FromUsername = 564987631;
ID = 6;
IndvMarks = "3;4;4;3;5;4;4;3;3;5";
Marks = 38;
Name = name3sv;
OutOF = 100;
ProfilePhoto = "2D0B36F3-71B3-4B5F-A375-7AD4D0AFF1AF.jpg\n";
TOname = hasnam;
ToUserName = 564987631;
Types = dsfv;
UserID = 564987631;
fromname = hasnam;
},
{
CamelID = 236;
Color = wejr;
Comments = "Jfj ki idhvjfn isms isn't if jfj Jen hik";
CompID = 235;
DateOfBirth = "/Date(1427058000000)/";
Description = jwenrfernkgr;
FromUserID = 564987631;
FromUsername = 564987631;
ID = 21;
IndvMarks = "8;6;10;10;10;7;5;8;9;5";
Marks = 78;
Name = name;
OutOF = 100;
ProfilePhoto = "E7735AF4-8EAB-41E7-A3F2-3280DBED0389.jpg\n";
TOname = lukman;
ToUserName = 564987634;
Types = wenjewfn;
UserID = 564987634;
fromname = hasnam;
}
)
答案 0 :(得分:1)
将值获取到数组中并使用排序描述符对它们进行排序。
NSArray *array = [dictioanry objectForKey:@"title"];
NSSortDescriptor* marksDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Marks"
ascending:YES];
NSArray* sortedObjects = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:marksDescriptor, nil]];
//YOu can put this object back into NSDictionary if you want it for later
我认为你的词典应该是这样的。
因此上面的代码应该可以对值进行排序。以下是我进行测试的示例代码。
注意:将您的JSON字符串保存在test.json中并将其添加到您的项目中。 or download the file from here并将其添加到您的项目中
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSDictionary *jsonDictionary=[NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:nil];
NSLog(@"%@",jsonDictionary);
NSArray *array = [jsonDictionary objectForKey:@"title"];
NSSortDescriptor* marksDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Marks"
ascending:YES];
NSArray* sortedObjects = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:marksDescriptor, nil]];
NSLog(@"%@",sortedObjects);
答案 1 :(得分:0)
示例:
NSArray *originalArray = @[
@{@"page_no": @"27"},
@{@"page_no": @"1"},
@{@"page_no": @"12"},
@{@"page_no": @"23"},
@{@"page_no": @"3"},
@{@"page_no": @"2"},
@{@"page_no": @"17"},
];
NSSortDescriptor *alphaNumSD = [NSSortDescriptor sortDescriptorWithKey:@"page_no"
ascending:YES
comparator:^(NSString *string1, NSString *string2)
{
return [string1 compare:string2 options:NSNumericSearch];
}];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:@[alphaNumSD]];
NSLog(@"%@", sortedArray);
输出:
(
{
"page_no" = 1;
},
{
"page_no" = 2;
},
{
"page_no" = 3;
},
{
"page_no" = 12;
},
{
"page_no" = 17;
},
{
"page_no" = 23;
},
{
"page_no" = 27;
}
)
它绝对有效。