Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 按值(非键)按字母顺序对NSMutableDictionary排序_Objective C_Sorting_Nsmutabledictionary - Fatal编程技术网

Objective c 按值(非键)按字母顺序对NSMutableDictionary排序

Objective c 按值(非键)按字母顺序对NSMutableDictionary排序,objective-c,sorting,nsmutabledictionary,Objective C,Sorting,Nsmutabledictionary,我有一个字典,它将学生id存储为键,将其显示名称存储为子字典“display”的键。字典看起来像这样: id-1: display: mark id-2: display: alexis id-3: display: beth PlistManager *pm = [[PlistManager alloc] init]; NSMutableDictionary *students = [pm getStudentsDict:ClassID]; NSMutableArray *sor

我有一个字典,它将学生id存储为键,将其显示名称存储为子字典“display”的键。字典看起来像这样:

id-1:
  display: mark
id-2:
  display: alexis
id-3:
  display: beth
PlistManager *pm = [[PlistManager alloc] init];
NSMutableDictionary *students = [pm getStudentsDict:ClassID];
NSMutableArray *sortedKeys = [[students allKeys] mutableCopy]; // remember to manually release the copies you create
[sortedKeys sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString *student1 = [students objectForKey:obj1];
    NSString *student2 = [students objectForKey:obj2];
    return [student1 compare:student2]; // this does a simple comparison, look at the NSString documentation for more options
}];
// at this point your sortedKeys variable contains the keys sorted by the name of the student they point to //
// if you want to create the other array you can do so like this:
NSArray *sortedStudents = [students objectsForKeys:sortedKeys notFoundMarker:[NSNull null]];

// you can also iterate through the students like so:
for (int i = 0; i < sortedKeys.count; ++i)
{
    NSString *key = sortedKeys[i];
    NSString *student = [students objectForKey:key];
}

// or access directly:
NSString *studentAtIndex3 = [students objectForKey:sortedKeys[3]];

// always remember to release or autorelease your copies //
[sortedkeys release];
我想把列表分成两个数组,一个用于键,一个用于值,看起来像这样

key   value
id-2  alexis
id-3  beth
id-1  mark
我目前有以下代码:

-(void)alphabetize {
    PlistManager *pm = [[PlistManager alloc] init];
    NSMutableDictionary *students = [pm getStudentsDict:ClassID];;
    NSMutableArray *keyArray = [[NSMutableArray alloc] init];
    NSMutableArray *valArray = [[NSMutableArray alloc] init];

    for (id key in students) {
        [keyArray addObject:key];
        [valArray addObject:[[students objectForKey:key] objectForKey:@"display"]];
    }

    NSSortDescriptor *alphaDescriptor = [[NSSortDescriptor alloc] initWithKey:@"DCFProgramName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
    NSArray *sortedValues = [valArray sortedArrayUsingDescriptors:[NSMutableArray arrayWithObjects:alphaDescriptor, nil]];
    NSLog(@"%@", sortedValues);
}
但是它在创建sortedValues数组时抛出了一个错误


如果有人能帮助我或者为我指出正确的方向,我将不胜感激。谢谢大家!

您必须根据它们在字典中链接的值对keys数组进行排序,然后创建第二个数组,尽管我觉得您实际上不需要第二个数组。实现所需功能的一种方法是使用
NSMutableArray
中的
sortUsingComparator:
方法,如下所示:

id-1:
  display: mark
id-2:
  display: alexis
id-3:
  display: beth
PlistManager *pm = [[PlistManager alloc] init];
NSMutableDictionary *students = [pm getStudentsDict:ClassID];
NSMutableArray *sortedKeys = [[students allKeys] mutableCopy]; // remember to manually release the copies you create
[sortedKeys sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString *student1 = [students objectForKey:obj1];
    NSString *student2 = [students objectForKey:obj2];
    return [student1 compare:student2]; // this does a simple comparison, look at the NSString documentation for more options
}];
// at this point your sortedKeys variable contains the keys sorted by the name of the student they point to //
// if you want to create the other array you can do so like this:
NSArray *sortedStudents = [students objectsForKeys:sortedKeys notFoundMarker:[NSNull null]];

// you can also iterate through the students like so:
for (int i = 0; i < sortedKeys.count; ++i)
{
    NSString *key = sortedKeys[i];
    NSString *student = [students objectForKey:key];
}

// or access directly:
NSString *studentAtIndex3 = [students objectForKey:sortedKeys[3]];

// always remember to release or autorelease your copies //
[sortedkeys release];
PlistManager*pm=[[PlistManager alloc]init];
NSMutableDictionary*students=[pm GetStudentsDictionary:ClassID];
NSMutableArray*sortedKeys=[[students allKeys]mutableCopy];//请记住手动释放您创建的副本
[sortedKeys sortUsingComparator:^n比较结果(id obj1,id obj2){
NSString*student1=[students objectForKey:obj1];
NSString*student2=[students objectForKey:obj2];
return[student1 compare:student2];//这是一个简单的比较,有关更多选项,请参阅NSString文档
}];
//此时,sortedKeys变量包含按指向的学生姓名排序的键//
//如果要创建另一个阵列,可以这样做:
NSArray*sortedStudents=[学生对象标记:sortedKeys notFoundMarker:[NSNull]];
//您也可以像这样反复浏览学生:
对于(int i=0;i

希望有帮助。

我认为您不能将
NSSortDescriptor
NSString
数组一起使用。考虑使用<代码> SoTyDrayRaysUnter选择器<代码> > NSArray < /Cord>。如何排序这两个数组,以便键和对象在排序后匹配?错误是什么“扔”?好吧,你不能排序字典…您可以对allValues数组进行排序。。。这似乎很容易。。。但它不会改变字典。非常感谢,它很有魅力。我一个人永远也弄不明白。