【问题标题】:how to Json Dictionary Array value sorting like (A-z, 0-9 and special char) in Swift [duplicate]如何在 Swift 中对 Json 字典数组值进行排序,如(A-z、0-9 和特殊字符)[重复]
【发布时间】:2017-12-30 19:37:56
【问题描述】:

我试着排序

let students: NSMutableArray = [["fullName": "23412334", "number": "39485793"],["fullName": "&234@fwv", "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 = "&234@fwv";
            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 = "&234@fwv";
            number = "395793";
    },
  )

请帮我如何排序.... 非常感谢您提前....

【问题讨论】:

  • 您的previous question 中有解决方案。顺便说一句:不要在 Swift 中使用 NSMutable... 集合类型。它们缺少类型信息,并且与 Swift 原生类型无关。
  • 是的..我试过了,但没有用....请帮助我@vadian

标签: ios json swift nsdictionary


【解决方案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": "&234@fwv", "number": "395793"],["fullName": "Abena", "number": "3572343"],["fullName": "Peter", "number": "394568993"],["fullName": "Kweku", "number": "309693"]]
let sortedResults = getSpecialSorted(arrayOfStudents: students)
    print(sortedResults)

【讨论】:

  • 非常感谢......@Mohammad Sadiq
猜你喜欢
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
相关资源
最近更新 更多