用两个特殊键对NSmutablearray进行排序 [英] Sort NSmutablearray with two special keys

查看:81
本文介绍了用两个特殊键对NSmutablearray进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableview,它的头存储在一个mutablearray中,该数组看起来像

I have a tableview, its header is stored in a mutablearray, the array looks like

(2005 fall, 2005 spring, 2007 summer...)

当我输出表格视图时,我希望标题按时间升序显示.

When I output the tableview, I want the header in time ascending displayed.

2005 spring
2005 fall
2007 summer

我在这里使用了代码:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    [self.sectionKeys sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *key = [self.sectionKeys objectAtIndex:section];
    return key;
}

它与year一起正常工作.但是,由于字母原因,fallspringsummer之前,请问该如何解决?

It works fine with year. However, fall comes before spring and summer because of alphabetreason , what to do to fix it please?

推荐答案

因此,您有一个带有分区键的数组.但是这些部分不是按数组顺序排列的,因此需要对其进行排序.您会注意到cellForRowAtIndexPath:需要完全相同的信息.所以在这个地方分类是错误的.

So you have an array with section keys. But the sections are not in order of the array, they need to be sorted. You will notice that cellForRowAtIndexPath: needs the exact same information. So sorting in this place is wrong.

该如何处理:我有一个属性"unsortedSectionKeys"和一个属性"sortedSectionKeys". sortedSectionKeys具有一个吸气剂,用于检查nil并存储unsortedSectionKeys的已排序副本(如果为nil).每当unsortedSectionKeys更改时,您只需将sortedSectionKeys设置为nil即可. (这至少解决了一些问题).

What I do to handle this: I have a property "unsortedSectionKeys" and a property "sortedSectionKeys". sortedSectionKeys has a getter that checks for nil and stores a sorted copy of unsortedSectionKeys if it is nil. And whenever unsortedSectionKeys changes, you just set sortedSectionKeys to nil. (That solves at least some problems).

为进行排序,您需要编写适当的代码.使用(void)sortUsingComparator:(NSComparator)cmptr对可变变量进行排序,或使用-(NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr获取数组的排序副本.

For your sorting, you need to write proper code. Use (void)sortUsingComparator:(NSComparator)cmptr to sort a mutable, or - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr to get a sorted copy of an array.

示例:

[self.sectionKeys sortArrayUsingComparator:^NSComparisonResult(NSString* obj1, NSString* obj2) {
    NSInteger year1 = obj1.integerValue;
    NSInteger year2 = obj2.integerValue;
    if (year1 < year2) return NSOrderedAscending;
    if (year1 > year2) return NSOrderedDescending;

    NSInteger season1 = 0;
    if ([obj1 rangeOfString:@"spring" options:NSCaseInsensitiveSearch].location != NSNotFound)
        season1 = 1;
    if ([obj1 rangeOfString:@"summer" options:NSCaseInsensitiveSearch].location != NSNotFound)
        season1 = 2;
    if ([obj1 rangeOfString:@"fall" options:NSCaseInsensitiveSearch].location != NSNotFound)
        season1 = 3;
    if ([obj1 rangeOfString:@"winter" options:NSCaseInsensitiveSearch].location != NSNotFound)
        season1 = 4;

    NSInteger season2 = 0;
    if ([obj2 rangeOfString:@"spring" options:NSCaseInsensitiveSearch].location != NSNotFound)
        season2 = 1;
    if ([obj2 rangeOfString:@"summer" options:NSCaseInsensitiveSearch].location != NSNotFound)
        season2 = 2;
    if ([obj2 rangeOfString:@"fall" options:NSCaseInsensitiveSearch].location != NSNotFound)
        season2 = 3;
    if ([obj2 rangeOfString:@"winter" options:NSCaseInsensitiveSearch].location != NSNotFound)
        season2 = 4;

    if (season1 < season2) return NSOrderedAscending;
    if (season1 > season2) return NSOrderedDescending;
    return NSOrderedSame;
}];

您的决定是冬季是一年中的第一个季节还是最后一个季节,因为通常是12月至2月.

Your decision if winter is the first or last season in the year, since usually it's December to February.

这篇关于用两个特殊键对NSmutablearray进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆