gpt4 book ai didi

ios - 字母数字数组排序

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:41 25 4
gpt4 key购买 nike

我有一个字母数字名称列表,当我运行下面的代码时,数字排在 A-Z (0-9 A-Z) 之前。

如何让这个 NSSortDescriptor 将数字放在数组 (A-Z 0-9) 的末尾?

NSSortDescriptor *sortNameSortDescriptor = 
[NSSortDescriptor sortDescriptorWithKey:@"sortName"
ascending:YES
selector:@selector(localizedStandardCompare:)];

NSArray *sortedArray = [sortSectionsArray sortedArrayUsingDescriptors:@[ sortNameSortDescriptor ]];

最佳答案

或许您可以使用基于 NSComparator 的排序描述符?

大纲

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"sortName" ascending:YES
comparator:^NSComparisonResult(NSString* obj1, NSString* obj2)
{
//Return negative number, zero or positive based on
//first letter being number or alpha.
//If obj1 starts with a letter and obj2 starts with a number then
//return NSOrderedAscending. . . otherwise just return [obj1 comppare:obj2];

//To test if first chararacter is a letter:

NSRange first = [obj1 rangeOfComposedCharacterSequenceAtIndex:0];
NSRange match = [obj1 rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] options:0 range:first];
if (match.location != NSNotFound) {//Implement above comment}
}];

关于ios - 字母数字数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19488555/

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