Ios 核心数据排序

Ios 核心数据排序,ios,core-data,Ios,Core Data,我有一些混合了字符串和整数的数据 "003G" "002P" "001P" "018P" "002G" "019P" "001G" "020P" "012P" "011P" "012G" "013P" "007P" "011G" "010P" "009P" "008P" "005P" "006P" "014P" "007G" "010G" "009G" "008G" "015P" "006G" "005Ga" "004P" "016P" "005G" "004G" "003P" "017P"

我有一些混合了字符串和整数的数据

"003G"
"002P"
"001P"
"018P"
"002G"
"019P"
"001G"
"020P"
"012P"
"011P"
"012G"
"013P"
"007P"
"011G"
"010P"
"009P"
"008P"
"005P"
"006P"
"014P"
"007G"
"010G"
"009G"
"008G"
"015P"
"006G"
"005Ga"
"004P"
"016P"
"005G"
"004G"
"003P"
"017P"
需要如下输出:

"001P"
"002P"
"003P"
"004P"
"005P"
"006P"
"007P"
"008P"
"009P"
"010P"
"011P"
"012P"
"013P"
"014P"
"015P"
"016P"
"017P"
"018P"
"019P"
"020P"
"001G"
"002G"
"003G"
"004G"
"005G"
"005Ga"
"006G"
"007G"
"008G"
"009G"
"010G"
"011G"
"012G"
同时Android用
*[0-9,0P-9P,0G-9G]

NSArray *keysArray;  // your strings
将每个字符串放入数组并使用下面的代码

NSArray *sortedArray = [keysArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Apple为字母排序提供了多个选择器:

compare:
caseInsensitiveCompare:
localizedCompare:
localizedCaseInsensitiveCompare:
localizedStandardCompare:

如果您使用NSFetchRequest查询核心数据,请继续在fetchRequest上添加排序描述符,如下所示:

fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"referenceNo" ascending:YES selector:@selector(caseInsensitiveCompare:)]];

这是一个非常不寻常的排序顺序。您必须使用
比较器编写自定义描述符

需要两个描述符

  • 将第四个字符降序排序

    let sortDescriptor1 = NSSortDescriptor(key: "referenceNo", ascending: false) { (obj1, obj2)  -> ComparisonResult in
        let string1 = obj1 as! String
        let string2 = obj2 as! String
        let fourthChar1 = string1.index(string1.startIndex, offsetBy: 3)
        let fourthChar2 = string2.index(string2.startIndex, offsetBy: 3)
        return String(string1[fourthChar1]).compare(String(string2[fourthChar2]))
    }
    
  • > p>排序前3个字符用<代码>数值< /代码>选项,并考虑<代码> XXXXA CASE

    let sortDescriptor2 = NSSortDescriptor(key: "referenceNo", ascending: true) { (obj1, obj2)  -> ComparisonResult in
        let string1 = obj1 as! String
        let string2 = obj2 as! String
        let fourthChar1 = string1.index(string1.startIndex, offsetBy: 3)
        let fourthChar2 = string2.index(string2.startIndex, offsetBy: 3)
        let orderedResult = string1.substring(to: fourthChar1).compare(string2.substring(to: fourthChar2), options: .numeric)
        if orderedResult == .orderedSame {
            return string1.characters.count < string2.characters.count ? .orderedAscending : .orderedDescending
        } else {
            return orderedResult
        }
    }
    
    让sortdescriptor 2=NSSortDescriptor(键:“referenceNo”,升序:true){(obj1,obj2)->在
    设string1=obj1为!字符串
    设string2=obj2为!String
    设fourthChar1=string1.index(string1.startIndex,offsetBy:3)
    设fourthChar2=string2.index(string2.startIndex,offsetBy:3)
    让orderedResult=string1.substring(to:fourthChar1)。比较(string2.substring(to:fourthChar2),选项:。数值)
    如果orderedResult==.orderedSame{
    返回string1.characters.count

    当然,这假设值是ASCII范围内始终包含4个或更多字符的字符串。

    此外,您所说的“排序CoreData”是什么意思,对从
    CoreData检索的数组进行排序?
    ?我有基于键的排序:NSSortDescriptor*sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“referenceNo”升序:是];输出类似:“001G”“001P”“002G”“002P”“003G”“003P”“004G”“004P”“005G”“005G”“005G”“006P”“007G”“007P”“008G”“008P”“009G”“009P”“010G”“010P”“011G”“012P”“013P”“014P”“015P”“015P”“017P”“018P”“019P”“020P”对我来说似乎不起作用,排序后的结果与Swift中的正常结果一样。有人用这种方式成功过吗?肯定对你有用。我的问题是您是否给出了错误的数组@ovo
    print(nsTexts.sortedArray)(使用:#选择器(NSString.localizedCaseInsensitiveCompare(:)))
    ,然后结果是
    [001G,001P,002G,002P,003G,003P,004G,004P….
    。所以…它不起作用。你的结果是正确的,人,…它和字典里的一样。例如顺序:aaa aba abb abc aca ada。方法的结果是正确的,但没有回答Jinu的问题。