2017-06-22 74 views
1

我有一些数据混合字符串和整数一样的,排序核心数据

"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]

+1

你到目前为止尝试过什么? –

+1

另外,你是什么意思“排序CoreData”,排序从'CoreData'检索的数组? –

+0

我根据关键字排序: NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@“referenceNo”ascending:YES]; 输出是这样的: “001G” “001P” “002G” “002P” “003G” “003P” “004G” “004P” “005G” “005Ga” “005P” “006G” “006P” “007G” “007P” “008G” “008P” “009G” “009P” “010G” “010P” “011G” “011P” “012G” “012P” “013P” “014P” “015P” “016P” “017P” “018P” “019P” “020P” – Jinu

回答

2

这是一个非常不寻常的排序顺序。您必须使用Comparator

编写自定义描述符需要两个描述符。

  1. 排序的第四个字符下降

    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])) 
    } 
    
  2. 排序的前3个字符与numeric选项上升,并考虑xxxxa情况

    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 
        } 
    } 
    

当然这个假设值是总是4个字符和mor的字符串e在ASCII范围内。

0
NSArray *keysArray; // your strings 
排序

将每个字符串放入数组中,并使用下面的代码

NSArray *sortedArray = [keysArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

苹果提供字母排序几个选择:

compare: 
caseInsensitiveCompare: 
localizedCompare: 
localizedCaseInsensitiveCompare: 
localizedStandardCompare: 
+0

它似乎不适合我的工作,导致后和Swift中的正常一样排序。有没有人有这样的成功? – ovo

+0

它肯定会为你工作。我是你给错了数组@ovo – Saranjith

+0

'print(nsTexts.sortedArray(using:#selector(NSString.localizedCaseInsensitiveCompare(_ :))))',then result is'[001G,001P,002G,002P,003G,003P, 004G,004P ....'。所以...这不起作用。 – ovo

0

如果使用NSFetchRequest查询核心数据,然后继续前进,在fetchRequest添加排序描述符象下面这样:

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