0

I tried to sort

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";
    }
  )

But I need:

 // Output:
( 
    {
            fullName = "Abena";
            number = "3572343";
    },
        {
            fullName = "Kweku";
            number = "309693";
    },
        {
            fullName = "Peter";
            number = "394568993";
    },
    {
            fullName = "23412334";
            number = "39485793";
    },
    {
            fullName = "&234@fwv";
            number = "395793";
    },
  )

Please help me how to sort this.... Thanking you very much in advance....

Rajamohan S
  • 7,229
  • 5
  • 36
  • 54
joel prithivi
  • 340
  • 2
  • 13
  • There is a solution in your [previous question](https://stackoverflow.com/questions/45277831/the-array-value-should-be-sort-like-alphabetic-numbers-and-special-characters). By the way: Do not use `NSMutable...` collection types in Swift. They lack the type information and are not related to the Swift native types. – vadian Jul 25 '17 at 04:23
  • Yes.. I tried it but not working.... please help me @vadian – joel prithivi Jul 25 '17 at 04:36

1 Answers1

1

Here you go

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
}

Also you need to change your declaration like

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
  • 5,070
  • 28
  • 29