OStack程序员社区-中国程序员成长平台

标题: ios - 使用比较器对 NSDictionaries 的 NSArray 进行排序 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:08
标题: ios - 使用比较器对 NSDictionaries 的 NSArray 进行排序

我一直在尝试使用比较器对 NSDictionaries 的 NSArray 进行排序,但我似乎无法获得我想要的输出。

我想要实现的输出是 A-Z 用户名应该排在排序数组的第一位,然后以数字开头的用户名应该排在排序数组的第二位,最后以下划线开头的用户名应该排在最后排序后的数组。任何帮助都非常感谢!

编辑:应该对其进行排序,使其在整个 NSArray 中看起来一致,这样:_Anna 位于 _Bob 之前,_11Bob 位于 _12Cary 之前但在 _09Bob 之后

我正在寻找的期望输出示例:

(
        {
        username = abcd;
    },
        {
        username = Anna;
    },
        {
        username = 01Bob;
    },
        {
        username = 02Tob;
    },
        {
        username = 03ZED;
    },
        {
        username = 04_Hob;
    },
        {
        username = 04_sob;
    },
        {
        username = "_anna";
    },
        {
        username = "_bob";
    },
        {
        username = "_boc";
    },
        {
        username = "_bocd12";
    },
        {
        username = "_bocd13";
    }
        {
        username = _01Bob;
    },
        {
        username = _02Tob;
    },
)

我希望现在有意义。

带有 NSDictionaries 的 NSArray 的示例 NSDictionary:

NSDictionary *dictionary = @{@"users"[@{@"username""191anna"},@{@"username""_091bob"},@{@"username""Bob"},@{@"username""charlie"}]};

我正在尝试使用这个比较器:

NSArray *array = [[dictionary objectForKey"users"] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
    NSString *f1 = [obj1 objectForKey"username"];
    NSString *f2 = [obj2 objectForKey"username"];

    NSString *s1 = [[obj1 objectForKey"username"]substringFromIndex:1];
    NSString *s2 = [[obj2 objectForKey"username"]substringFromIndex:1];

    if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == [s2 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location)
    {
        return  [f1 localizedCaseInsensitiveCompare:f2];
    }
    else if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != [s2 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location)
    {
        return  [f1 localizedCaseInsensitiveCompare:f2];

        if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == NSNotFound)
        {
            return NSOrderedDescending;
        }
    }
     return  NSOrderedAscending;
}];

但它给了我以下(不是我想要的)排序的 NSArray:

(
        {
        username = "_091bob";
    },
        {
        username = 191anna;
    },
        {
        username = Bob;
    },
        {
        username = charlie;
    }
)



Best Answer-推荐答案


这就是我想出的。它有点长,因为它需要相当多的逻辑。它可能会进一步优化:

我的设置:

NSArray * usernames = @[@"191anna", @"abcd", @"Anna", @"01Bob", @"02Tob", @"03ZED", @"04_rob", @"_anna", @"_bob", @"_boc", @"_bocd12", @"_bocd13", @"_01Bob", @"_02Tob"];
NSMutableArray * users = [NSMutableArray array];
for (NSString * username in usernames) {
    [users addObject:@{@"username":username}];
}
NSDictionary * dictionary = @{@"users":users};

排序:

NSArray *sortedArray = [dictionary[@"users"] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
    NSString *nameOne = obj1[@"username"];
    NSString *nameTwo = obj2[@"username"];

    NSString *startOne;
    NSString *startTwo;

    NSInteger currentIndex = 0;
    NSInteger maxIndex = (nameOne.length < nameTwo.length) ? nameOne.length : nameTwo.length;

    // Get our first differentiating letter
    do {
        if (currentIndex < maxIndex) {

            startOne = [nameOne substringWithRange:NSMakeRange(currentIndex, 1)];
            startTwo  = [nameTwo substringWithRange:NSMakeRange(currentIndex, 1)];
            currentIndex++;
        }
        else {

            // Names are equal up to max length. Same length is same, else shorter word ascending.  (bob above bobb)
            if (nameOne.length == nameTwo.length) {
                return NSOrderedSame;
            }
            else {
                return (nameOne.length < nameTwo.length) ? NSOrderedAscending : NSOrderedDescending;
            }
        }

    } while ([startOne isEqualToString:startTwo]);
    // Prioritize underscores to bottom
    NSCharacterSet * underscoreCharSet = [NSCharacterSet characterSetWithCharactersInString:@"_"];

    NSRange underscoreRangeOne = [startOne rangeOfCharacterFromSet:underscoreCharSet];
    NSRange underscoreRangeTwo = [startTwo rangeOfCharacterFromSet:underscoreCharSet];

    if (underscoreRangeOne.length > 0 || underscoreRangeTwo.length > 0) {
        // Something is underscored, put it on the bottom
        return (underscoreRangeOne.length > 0) ? NSOrderedDescending : NSOrderedAscending;
    }
    // Prioritize numbers to bottom
    NSRange decimalRangeOne = [startOne rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
    NSRange decimalRangeTwo = [startTwo rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
    if (decimalRangeOne.length > 0 || decimalRangeTwo.length > 0) {
        // Something is numbered, put it on the bottom
        if (decimalRangeOne.length == decimalRangeTwo.length) {
            return (startOne.intValue > startTwo.intValue) ? NSOrderedDescending : NSOrderedAscending;
        }
        else if (decimalRangeOne.length > decimalRangeTwo.length) {
            return NSOrderedDescending;
        }
        else if (decimalRangeTwo.length > decimalRangeOne.length) {
            return NSOrderedAscending;
        }

    }

    // Now, sort alphabetically
    return  [nameOne localizedCaseInsensitiveCompare:nameTwo];

}];

NSLog(@"SortedArray: %@", sortedArray);

将记录为:

abcd,
Anna,
01Bob,
02Tob,
03ZED,
"04_rob",
191anna,
"_anna",
"_bob",
"_boc",
"_bocd12",
"_bocd13",
"_01Bob",
"_02Tob"

关于ios - 使用比较器对 NSDictionaries 的 NSArray 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964377/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4