I want to sort an array using NSSortDescriptor

前端 未结 7 2330
感情败类
感情败类 2020-12-04 15:04

I am having a problem regarding sorting an array w.r.t database:

NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@\"w\" ascending:YES];
NSAr         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-04 15:45

    I think this will do the trick for you. The docs for it are here: String Programming Guide

    Add this little function written by Apple.

    int finderSortWithLocale(id string1, id string2, void *locale)
    {
        static NSStringCompareOptions comparisonOptions =
            NSCaseInsensitiveSearch | NSNumericSearch |
            NSWidthInsensitiveSearch | NSForcedOrderingSearch;
    
        NSRange string1Range = NSMakeRange(0, [string1 length]);
    
        return [string1 compare:string2
                        options:comparisonOptions
                        range:string1Range
                        locale:(NSLocale *)locale];
    }
    

    Make sure that you copy the function definition into your header, or you'll get a compile error on your sorted array.

    For your sorted array, use this method:

    [mGlossaryArray sortedArrayUsingFunction:finderSortWithLocale context:[NSLocale currentLocale]];
    

    Your results will look like this:

    • c
    • cabin
    • cafe
    • Cancer
    • Chinese
    • Christianity
    • Christmas
    • Coke

提交回复
热议问题