使用 NSDictionary 对 NSArray 进行排序

发布于 2024-11-18 14:01:28 字数 897 浏览 2 评论 0原文

我正在使用 NSSortDiscriptors 对 NSArray 进行排序。 数组中的每个索引都包含一个带有键、值条目的字典。这本词典还包含另一本词典。

我想要解决的问题是根据第一个字典上的值对数组进行排序,同时也根据它包含的字典中的一些值对数组进行排序。

以下是我对数组中其他值使用的排序方法。

- (void)sortArray {

if ([array count] != 0) {

    // Reports sortingDescriptors
    NSSortDescriptor * city = [[NSSortDescriptor alloc] initWithKey:@"City" ascending:YES];
    NSSortDescriptor * name = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
    NSSortDescriptor * country = [[NSSortDescriptor alloc] initWithKey:@"Country" ascending:YES];

    [reportsArray sortUsingDescriptors:[NSArray arrayWithObjects:city, name, country, nil]];

    [name release];
    [city release];
    [country release];
}
}

该数组看起来像这样:

[{name = "";
city = "";
country = "";
date = {
 dateAdded = "";
 dateRemoved = "";
}
}];

例如,如果在 dateAdded 上有值,我也想进行排序。

I'm using NSSortDiscriptors to sort an NSArray.
Every index in the array contains a dictionary with key,value entries. This dictionary also holds another dictionary.

My problem that I want solve is to sort the array based on values on the first dictionary, but also on some values in the dictionary it contains.

The following is the sort method I use for the other values in the array.

- (void)sortArray {

if ([array count] != 0) {

    // Reports sortingDescriptors
    NSSortDescriptor * city = [[NSSortDescriptor alloc] initWithKey:@"City" ascending:YES];
    NSSortDescriptor * name = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
    NSSortDescriptor * country = [[NSSortDescriptor alloc] initWithKey:@"Country" ascending:YES];

    [reportsArray sortUsingDescriptors:[NSArray arrayWithObjects:city, name, country, nil]];

    [name release];
    [city release];
    [country release];
}
}

The array looks like this:

[{name = "";
city = "";
country = "";
date = {
 dateAdded = "";
 dateRemoved = "";
}
}];

So I also want to sort on, if have value on dateAdded for example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扛刀软妹 2024-11-25 14:01:28

您可以在创建NSSortDescriptor时指定关键路径,这样您就可以使用NSDictionaryNSArray进行排序。

You can specify a key path when you create the NSSortDescriptor, this way you can sort the NSArray with a NSDictionary.

情话难免假 2024-11-25 14:01:28

您可能想检查字典是否包含这样的值:

// you can't set nil as a value for a key
if([yourDictionary objectForKey:@"yourKey"] == [NSNull null]) { ... }

然后您需要对字典之外的剩余对象进行排序,为此,请通过执行以下操作来复制没有字典条目的数组副本:

NSMutableArray *tmpArray = [NSMutableArray arrayWithArray:firstArray];
[tmpArray removeObjectAtIndex:theIndexOfTheDictionary];
// sort your array, don't forget to catch the returned value
NSMutableArray *sortedArray = [tmpArray sortUsingDescriptors:[NSArray arrayWithObjects:city, name, country, nil]];
// finally, put the dictionary back in (if needed)
[sortedArray insertObject:theDictionary atIndex:theIndexYouWant];

You might want to check if the dictionary contains a value like that :

// you can't set nil as a value for a key
if([yourDictionary objectForKey:@"yourKey"] == [NSNull null]) { ... }

Then you need to sort remaining objects but the dictionary, to do so, make a copy of your array without the dictionary entry by doing something like :

NSMutableArray *tmpArray = [NSMutableArray arrayWithArray:firstArray];
[tmpArray removeObjectAtIndex:theIndexOfTheDictionary];
// sort your array, don't forget to catch the returned value
NSMutableArray *sortedArray = [tmpArray sortUsingDescriptors:[NSArray arrayWithObjects:city, name, country, nil]];
// finally, put the dictionary back in (if needed)
[sortedArray insertObject:theDictionary atIndex:theIndexYouWant];
你怎么敢 2024-11-25 14:01:28

您是说数组中的对象具有 City、Name 和 Country 属性,而且还有字典属性,并且您想要对字典中的键之一进行排序?或者你是说数组中的条目是字典,但有时城市、名称或国家键丢失?或者你是说有些条目是字典,有些是具有列出属性的对象?

在任何情况下,您都可以通过使用 initWithKey:ascending:comparator:。这允许您提供一个比较器块作为排序函数,它比直接选择器更灵活,例如

NSComparator mySort = ^(id obj1, id obj2)
{
    NSComparisonResult ret = NSOrderedSame;

    if ([obj1 isKindOfClass: [NSDictionary class]] && ![obj2 isKindOfClass: NSDictionary class]])
    {
        ret = NSOrderedAscending;
    }
    else if (![obj1 isKindOfClass: [NSDictionary class]] && [obj2 isKindOfClass: NSDictionary class]])
    {
        ret = NSOrderedDescending;
    }
    return ret;
};
NSSortDescriptor* descriptor = [[NSSortDescriptor alloc] initWithKey: @"self" ascending: YES comparator: mySort];

将为您提供一个排序描述符,该描述符对数组进行排序,将所有 NSDictionaries 首先,然后再排序其他对象。 (self 是所有返回对象本身的 NSObject 所拥有的键)。

Are you saying that the objects in the array have a City, Name and Country property but also a dictionary property and you want to sort on one of the keys in the dictionary? Or are you saying that the entries in the array are dictionaries but sometimes the City, Name or Country key is missing? Or are you saying that some of the entries are dictionaries and some are objects with the listed properties?

In any case, you can get more flexibility by creating a sort descriptor using initWithKey:ascending:comparator:. This allows you to supply a comparator block as the sort function which is more flexible than a straight selector e.g.

NSComparator mySort = ^(id obj1, id obj2)
{
    NSComparisonResult ret = NSOrderedSame;

    if ([obj1 isKindOfClass: [NSDictionary class]] && ![obj2 isKindOfClass: NSDictionary class]])
    {
        ret = NSOrderedAscending;
    }
    else if (![obj1 isKindOfClass: [NSDictionary class]] && [obj2 isKindOfClass: NSDictionary class]])
    {
        ret = NSOrderedDescending;
    }
    return ret;
};
NSSortDescriptor* descriptor = [[NSSortDescriptor alloc] initWithKey: @"self" ascending: YES comparator: mySort];

will give you a sort descriptor that sorts the array putting all the NSDictionaries first and other objects afterwards. (self is a key possessed by all NSObjects that returns the object itself).

留一抹残留的笑 2024-11-25 14:01:28
NSArray *unsortedArray=[NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"anil",@"personInDictionary.lastName" ,nil],[NSDictionary dictionaryWithObjectsAndKeys:@"aneelu",@"personInDictionary.lastName", nil] ,[NSDictionary dictionaryWithObjectsAndKeys:@"kumar",@"anil.lastName", nil] ,nil];

 NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey:@"personInDictionary.lastName"             ascending:YES] autorelease]; // 1

  NSArray * sortedArray = [unsortedArray sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:descriptor]];

 NSLog(@"sortedArray values %@",sortedArray);

  for (id object in [sortedArray valueForKey:@"personInDictionary.lastName"]) {

        NSLog(@"sortedArray value %@",object);
    }
NSArray *unsortedArray=[NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"anil",@"personInDictionary.lastName" ,nil],[NSDictionary dictionaryWithObjectsAndKeys:@"aneelu",@"personInDictionary.lastName", nil] ,[NSDictionary dictionaryWithObjectsAndKeys:@"kumar",@"anil.lastName", nil] ,nil];

 NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey:@"personInDictionary.lastName"             ascending:YES] autorelease]; // 1

  NSArray * sortedArray = [unsortedArray sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:descriptor]];

 NSLog(@"sortedArray values %@",sortedArray);

  for (id object in [sortedArray valueForKey:@"personInDictionary.lastName"]) {

        NSLog(@"sortedArray value %@",object);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文