【问题标题】:Sorting arraylist in correct order in Objective C在Objective C中以正确的顺序对arraylist进行排序
【发布时间】:2013-12-27 11:41:17
【问题描述】:

我有一个数组键列表,我使用以下代码从字典中获取:

    NSArray*keys=[dictionary allKeys];

键值是这样来的:

Fact tags - 1                                     ,
Fact tags - 14                                    ,
Brochure                                          ,
Balloon                                           ,
Fact tags - 10                                    ,
Fact tags - 2                                     ,
Fact tags - 12                                    ,
Fact tags - 3                                     ,
Booklet                                           , 
Fact tags - 15                                    ,
Fact tags - 5                                     ,
Fact tags - 13                                    ,
Fact tags - 4                                     ,
Fact tags - 11                                    ,
Fact tags - 6                                     ,
Fact tags - 9                                     ,
Fact tags - 8                                     ,
Fact tags - 7                                     ,

所以,对于数组列表排序。我正在使用以下代码:

keys = [keys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

现在,我的键输出是:

Balloon                                           ,
Booklet                                           ,
Brochure                                          ,
Fact tags - 1                                     ,
Fact tags - 10                                    ,
Fact tags - 11                                    ,
Fact tags - 12                                    ,
Fact tags - 13                                    ,
Fact tags - 14                                    ,
Fact tags - 15                                    ,
Fact tags - 2                                     ,
Fact tags - 3                                     ,
Fact tags - 4                                     ,
Fact tags - 5                                     ,
Fact tags - 6                                     ,
Fact tags - 7                                     ,
Fact tags - 8                                     ,
Fact tags - 9                                     ,

这里是按字母顺序对数组进行排序,但我希望我的数组列表输出如下列表:

Balloon                                          ,
Booklet                                          ,
Brochure                                         ,
Fact tags - 1                                    ,
Fact tags - 2                                    ,
Fact tags - 3                                    ,
Fact tags - 4                                    ,
Fact tags - 5                                    ,
Fact tags - 6                                    ,
Fact tags - 7                                    ,
Fact tags - 8                                    ,
Fact tags - 9                                    ,
Fact tags - 10                                   ,
Fact tags - 11                                   ,
Fact tags - 12                                   ,
Fact tags - 13                                   ,
Fact tags - 14                                   ,
Fact tags - 15                                   ,

您能否告诉我,我需要在我的代码中对这种类型的排序进行哪些更改?

【问题讨论】:

    标签: ios arrays sorting


    【解决方案1】:

    最好的方法...

     keys = [keys sortedArrayUsingComparator:^(NSString* first, NSString* second){
    
             return [first compare:second options:NSNumericSearch | NSCaseInsensitiveSearch];
                }
            ];
    

    您也可以使用自定义对象代替NSString

    【讨论】:

    • 感谢您的回答@Sunny shah。
    【解决方案2】:

    你试过 sortedArrayUsingComparator,

    试试喜欢,

    keys = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
        }];
    

    【讨论】:

    • mcaYeah 当然..但我只能在 8 分钟后接受这个答案。现在还剩 5 分钟加入 stackoverflow。
    • @AnandGautam:好的,很高兴为您提供帮助。 :)
    • @AnandGautam:我想到了这一点,Tester aur Developer ki sirf Dushmani ho sakti he - Dhoom Style。哈哈哈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 2019-09-16
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多