字典键排序选项 - 字母然后数字

发布于 2025-01-06 22:42:47 字数 391 浏览 1 评论 0原文

所有,

我想对这个可变数组键字典进行排序,如下所示:AZ 0-9,但它返回排序0-9 AZ。如何对其进行排序,使字母字符位于数字之前?也许有一个内置方法可以做到这一点,或者我应该创建一个扩展 NSString 的类别?

    NSArray *sKeysArray = [[listContent allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    NSString *sectionKey = [sKeysArray objectAtIndex:section];
    NSArray *sectionValues = [listContent valueForKey:sectionKey];

All,

I would like to sort this mutable dictionary of arrays keys like so: A-Z 0-9 but it's coming back sorted 0-9 A-Z. How can I sort it such that the alpha chars come before the numbers? Perhaps there is a built-in method that does this or should I create a category that extends NSString?

    NSArray *sKeysArray = [[listContent allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    NSString *sectionKey = [sKeysArray objectAtIndex:section];
    NSArray *sectionValues = [listContent valueForKey:sectionKey];

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

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

发布评论

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

评论(2

魂归处 2025-01-13 22:42:47

遗憾的是,NSString 中没有方法可以直接执行此操作。我不编写类别,而是使用 NSArraysortedArrayUsingComparator: 方法:

NSArray *sKeysArray = [[listContent ALlKeys] sortedArrayUsingComparator:^(id obj1, id obj2) {
// Code to compare obj1 and obj2 and return an NSComparisonResult here; return NSOrderedAscending if obj1 should come before obj2 
}];

为了进行每次比较,我将使用 NSString– enumerateSubstringsInRange:options:usingBlock: 传递 NSStringEnumerationByComposedCharacterSequences 选项(基本上枚举字符,除了 unicode 字符序列实际上是一个“字母”的组合)。当您比较两个字符时,请使用类似于此问题<的答案/a> 检测 obj1 是否为数字且 obj2 是否为常规字母并返回 NSOrderedDescending,否则仅使用常规比较:。

Sadly there's no method in NSString that will directly do this. Instead of writing a category, I would just use NSArrays sortedArrayUsingComparator: method:

NSArray *sKeysArray = [[listContent ALlKeys] sortedArrayUsingComparator:^(id obj1, id obj2) {
// Code to compare obj1 and obj2 and return an NSComparisonResult here; return NSOrderedAscending if obj1 should come before obj2 
}];

To do each comparison, I would use NSString's – enumerateSubstringsInRange:options:usingBlock: passing NSStringEnumerationByComposedCharacterSequences for options (basically enumerating characters, except that unicode characters sequences which are actually one "letter" are combined). And when you're comparing two characters, use something like the answer to this question to detect when obj1 is a number and obj2 is regular letter and return NSOrderedDescending, otherwise just use regular compare:.

送君千里 2025-01-13 22:42:47

就像@yuri所说,sortedArrayUsingComparator:应该提供基本功能。

这是一个简单的版本,它将以十进制数字开头的字符串排序在以字母字符开头的字符串(在 NSCharacterSet 的定义中)之后,并将所有其他字符串组合保留为其默认排序顺序

NSArray *arr = [NSArray arrayWithObjects:
                 @"ABC 09 GHJ", @"DEF23PO", @"123ABC", @"MCMLXV", @"[cocoa]", @"$2000", @"ERT", @"24seven", @"zero",
                @"Ångström", @"Übernacht",
                nil];

NSCharacterSet *digits    = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *alphanums = [NSCharacterSet alphanumericCharacterSet];

NSArray *sorted = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    if ( [obj1 isKindOfClass:[NSString class]] && [obj1 length] > 0
        && [obj2 isKindOfClass:[NSString class]] && [obj2 length] > 0 ) {

        unichar c1 = [obj1 characterAtIndex:0];
        unichar c2 = [obj2 characterAtIndex:0];

        if ( [digits characterIsMember:c1] && (![digits characterIsMember:c2] && [alphanums characterIsMember:c2]) ) {
            return NSOrderedDescending;
        }

        if ( [digits characterIsMember:c2] && (![digits characterIsMember:c1] && [alphanums characterIsMember:c1]) ) {
            return NSOrderedAscending;
        }
    }

    return [obj1 compare: obj2];
}];

NSLog(@"Sorted: %@", sorted);

:它不会正确处理由十进制数字作为第一个组成部分组成的 unicode 字母,但我会认为这是一个病态的情况,直到有人能启发我。

Like @yuri says, sortedArrayUsingComparator: should provide the basic functionality.

Here is a simple version that sorts strings beginning with a decimal digit after strings beginning with an alphabetic character (in NSCharacterSet's definition), and leaves all other combinations of strings to their default sort order:

NSArray *arr = [NSArray arrayWithObjects:
                 @"ABC 09 GHJ", @"DEF23PO", @"123ABC", @"MCMLXV", @"[cocoa]", @"$2000", @"ERT", @"24seven", @"zero",
                @"Ångström", @"Übernacht",
                nil];

NSCharacterSet *digits    = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *alphanums = [NSCharacterSet alphanumericCharacterSet];

NSArray *sorted = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    if ( [obj1 isKindOfClass:[NSString class]] && [obj1 length] > 0
        && [obj2 isKindOfClass:[NSString class]] && [obj2 length] > 0 ) {

        unichar c1 = [obj1 characterAtIndex:0];
        unichar c2 = [obj2 characterAtIndex:0];

        if ( [digits characterIsMember:c1] && (![digits characterIsMember:c2] && [alphanums characterIsMember:c2]) ) {
            return NSOrderedDescending;
        }

        if ( [digits characterIsMember:c2] && (![digits characterIsMember:c1] && [alphanums characterIsMember:c1]) ) {
            return NSOrderedAscending;
        }
    }

    return [obj1 compare: obj2];
}];

NSLog(@"Sorted: %@", sorted);

Caveat: It will not handle a unicode letter composed of a decimal digit as the first component correctly, but I will consider that a pathological case until someone can enlighten me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文