2017-07-25 44 views
0

排序像(AZ,0-9和特殊字符),我试图梳理如何JSON字典数组值斯威夫特

let students: NSMutableArray = [["fullName": "23412334", "number": "39485793"],["fullName": "&[email protected]", "number": "395793"],["fullName": "Abena", "number": "3572343"],["fullName": "Peter", "number": "394568993"],["fullName": "Kweku", "number": "309693"]] 
let sortDescriptor = NSSortDescriptor(key: "fullName", ascending: true, selector: #selector(NSString.localizedCompare(_:))) 
let sortedResults: NSArray = students.sortedArray(using: [sortDescriptor]) as NSArray 
students = NSMutableArray(array: sortedResults) 
print(students) 

// Output: 
(
     { 
      fullName = "&[email protected]"; 
      number = "395793"; 
    }, 
     { 
      fullName = "23412334"; 
      number = "39485793"; 
    }, 
     { 
      fullName = "Abena"; 
      number = "3572343"; 
    }, 
     { 
      fullName = "Kweku"; 
      number = "309693"; 
    }, 
     { 
      fullName = "Peter"; 
      number = "394568993"; 
    } 
) 

但我需要:

// Output: 
( 
    { 
      fullName = "Abena"; 
      number = "3572343"; 
    }, 
     { 
      fullName = "Kweku"; 
      number = "309693"; 
    }, 
     { 
      fullName = "Peter"; 
      number = "394568993"; 
    }, 
    { 
      fullName = "23412334"; 
      number = "39485793"; 
    }, 
    { 
      fullName = "&[email protected]"; 
      number = "395793"; 
    }, 
) 

请帮助我如何这个排序....感谢你非常提前....

+0

你的宣言。在你的[前一个问题]溶液(https://stackoverflow.com/questions/45277831/the-array-value-should-be-sort样字母,数字,和特殊字符)。顺便说一句:不要在Swift中使用'NSMutable ...'集合类型。它们缺少类型信息,并且与Swift本机类型无关。 – vadian

+0

是的..我尝试过,但没有工作....请帮助我@vadian –

回答

1

在这里你去

func isDigit(c: UnicodeScalar) -> Bool { 
    let digits = CharacterSet.decimalDigits 
    return digits.contains(c) 
} 

func isAlphabet(c : UnicodeScalar) -> Bool { 
    let letters = CharacterSet.letters 
    return letters.contains(c) 
} 

func getSpecialSorted (arrayOfStudents : [[String : String]]) -> [[String : String]] { 
    var arrayStartingWithAlphabets = [[String : String]]() 
    var arrayStartingWithDigits = [[String : String]]() 
    var arrayStartingWithAlphanumeric = [[String : String]]() 
    for student in arrayOfStudents { 
     if isDigit(c: (student["fullName"]?.unicodeScalars.first!)!) { 
      arrayStartingWithDigits.append(student) 
     } else if isAlphabet(c:(student["fullName"]?.unicodeScalars.first!)!) { 
      arrayStartingWithAlphabets.append(student) 
     } else { 
      arrayStartingWithAlphanumeric.append(student) 
     } 
    } 
    arrayStartingWithAlphabets = getNormalSorted(array : arrayStartingWithAlphabets) 
    arrayStartingWithDigits = getNormalSorted(array: arrayStartingWithDigits) 
    arrayStartingWithAlphanumeric = getNormalSorted(array: arrayStartingWithAlphanumeric) 
    return arrayStartingWithAlphabets + arrayStartingWithDigits + arrayStartingWithAlphanumeric 
} 

func getNormalSorted(array : [[String : String]]) -> [[String: String]]{ 
    let sortedArray = array.sorted { (firstStudent, secondStudent) -> Bool in 
     if let firstName = firstStudent["fullName"] , let 
      secondName = secondStudent["fullName"] { 
      return firstName.compare(secondName) == ComparisonResult.orderedAscending 
     } 
     return false 
    } 
    return sortedArray 
} 

你也需要改变像

let students = [["fullName": "23412334", "number": "39485793"],["fullName": "&[email protected]", "number": "395793"],["fullName": "Abena", "number": "3572343"],["fullName": "Peter", "number": "394568993"],["fullName": "Kweku", "number": "309693"]] 
let sortedResults = getSpecialSorted(arrayOfStudents: students) 
    print(sortedResults) 
+0

非常感谢你.... @ Mohammad Sadiq –