gpt4 book ai didi

ios - 使用 NSSortDescriptor 排序

转载 作者:行者123 更新时间:2023-11-29 12:20:41 25 4
gpt4 key购买 nike

我在排序方面遇到了问题。

数据是这样的

NSArray* testArr = @[@{@"key":@"aaa", @"value":@"asdfaf"},
@{@"key":@"baa", @"value":@"bsdfaf"},
@{@"key":@"!!aaa", @"value":@"adsfdfaf"},
@{@"key":@"123aaa", @"value":@"cecdfaf"},
@{@"key":@"@@#21", @"value":@"a42faf"},
@{@"key":@"ace", @"value":@"a123faf"},
@{@"key":@"321!!", @"value":@"123sdfaf"},
];

而 NSSortDescriptor 是这样的

NSLocale *locale = [NSLocale autoupdatingCurrentLocale];        
NSSortDescriptor *sortUsingNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {

NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch | NSNumericSearch;
NSRange stringRange = NSMakeRange(0, ((NSString *)obj1).length);

return [(NSString*)obj1 compare:(NSString*) obj2 options:comparisonOptions range:stringRange locale:locale];
}];

这是结果。

{
key = "!!aaa";
value = adsfdfaf;
},
{
key = "@@#21";
value = a42faf;
},
{
key = 123aaa;
value = cecdfaf;
},
{
key = "321!!";
value = 123sdfaf;
},
{
key = aaa;
value = asdfaf;
},
{
key = ace;
value = a123faf;
},
{
key = baa;
value = bsdfaf;
}

但是我想这样排序

{
key = aaa;
value = asdfaf;
},
{
key = ace;
value = a123faf;
},
{
key = baa;
value = bsdfaf;
},
{
key = 123aaa;
value = cecdfaf;
},
{
key = "321!!";
value = 123sdfaf;
},
{
key = "!!aaa";
value = adsfdfaf;
},
{
key = "@@#21";
value = a42faf;
}

它是字母、数字和特殊字符序列。我该怎么做

最佳答案

    NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
NSArray* sortedArray = [inFileTypes sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

关于ios - 使用 NSSortDescriptor 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30752009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com