消除数组中的重复项并对结果进行排序

发布于 2024-12-22 04:57:40 字数 1221 浏览 1 评论 0原文

我有一个从 plist 中检索其信息的应用程序。 plist 是一个包含键(author、cat、content)的 Dictionaries 数组。 现在我想在表格中显示类别。 所以我需要以排序的方式所有独特的类别条目。

我已成功实施以下方式,但我很感兴趣这是否是“正确”的解决方案。

  1. 遍历 plist 数组并使用所有类别创建新的 NSMutableArray 字典的条目
  2. 将此 NSMutableArray 放入 NSSet 中以获取唯一元素
  3. 将此 NSSet 放入 NSArray 中以获取唯一 元素对其进行排序的可能性。
  4. 返回一个用排序后的 NSArray 启动的 NSMutableArray

我觉得不是,这是这样做的正确方法。 有什么建议可以做得更好吗?

多谢!

//InhaltefromWEb is a NSMutableArray
-(NSArray*) getCategories {
NSMutableArray* categorieTemp = [[NSMutableArray alloc] init];  

unsigned count = [InhalteFromWeb count];
while (count--) {
    NSString *tempString;
    tempString=[[InhalteFromWeb objectAtIndex:count] objectForKey:CATEGORY];
    NSLog(@"tempString %@", tempString );
    [categorieTemp addObject:tempString];
}

NSSet *uniqueElements = [NSSet setWithArray:categorieTemp];
NSLog(@"categories from engine %@", categorieTemp);
NSArray* tempAr = [[[NSArray alloc] initWithArray:[uniqueElements allObjects]]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

return  [NSMutableArray arrayWithArray:tempAr];

}

I have an app which is retrieving its information from a plist.
The plist is an array of Dictionaries with the keys (author, cat, content).
Now I would like to show the categories in a table.
So I need all unique category entries in a sorted way.

I have successfully implemented following way but I am interested if this is the "right" solution.

  1. Go through plist array and make new NSMutableArray with all category
    entries of the dictionary
  2. put this NSMutableArray into a NSSet to get unique Elements
  3. put this NSSet into an NSArray to have the possibility to sort it.
  4. return a NSMutableArray initiated with the sorted NSArray

I feel not , that this is the right way of doing so.
Any suggestions to do it better ?

Thanks a lot!

//InhaltefromWEb is a NSMutableArray
-(NSArray*) getCategories {
NSMutableArray* categorieTemp = [[NSMutableArray alloc] init];  

unsigned count = [InhalteFromWeb count];
while (count--) {
    NSString *tempString;
    tempString=[[InhalteFromWeb objectAtIndex:count] objectForKey:CATEGORY];
    NSLog(@"tempString %@", tempString );
    [categorieTemp addObject:tempString];
}

NSSet *uniqueElements = [NSSet setWithArray:categorieTemp];
NSLog(@"categories from engine %@", categorieTemp);
NSArray* tempAr = [[[NSArray alloc] initWithArray:[uniqueElements allObjects]]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

return  [NSMutableArray arrayWithArray:tempAr];

}

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

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

发布评论

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

评论(2

Oo萌小芽oO 2024-12-29 04:57:40

使用 [categorieTemp containsObject:tempString] 检查现有字符串,并使用 [categorieTemp sortUsingComparator:...] 对其进行排序

示例:

[categorieTemp sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
    return [(NSString *)obj1 caseInsensitiveCompare:(NSString *)obj2];
}];

Check for existing String with [categorieTemp containsObject:tempString] and sort it with [categorieTemp sortUsingComparator:...]

Example:

[categorieTemp sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
    return [(NSString *)obj1 caseInsensitiveCompare:(NSString *)obj2];
}];
心的位置 2024-12-29 04:57:40

我这样做是为了对数组进行日期排序:

[array sortedArrayUsingComparator: ^(id obj1, id obj2) {
        NSDate *date1 = [obj1 objectForKey:@"datum"] == nil ? nilDate : [obj1 objectForKey:@"datum"];
        NSDate *date2 = [obj2 objectForKey:@"datum"] == nil ? nilDate : [obj2 objectForKey:@"datum"];
        return (NSComparisonResult)[date2 compare: date1];
    }];

I do this to sort my array for a date:

[array sortedArrayUsingComparator: ^(id obj1, id obj2) {
        NSDate *date1 = [obj1 objectForKey:@"datum"] == nil ? nilDate : [obj1 objectForKey:@"datum"];
        NSDate *date2 = [obj2 objectForKey:@"datum"] == nil ? nilDate : [obj2 objectForKey:@"datum"];
        return (NSComparisonResult)[date2 compare: date1];
    }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文