2

I tried to sort like the array as follows:

let students: Set = ["23412334","&234@fwv","Kofi", "Abena", "Peter", "Kweku", "Akosua"]
let sortedStudents = students.sorted()
print(sortedStudents)

Output:

["&234@fwv", "23412334", "Abena", "Akosua", "Kofi", "Kweku", "Peter"]

But I need like

["Abena", "Akosua", "Kofi", "Kweku", "Peter", "23412334","&234@fwv"]
halfer
  • 19,824
  • 17
  • 99
  • 186
joel prithivi
  • 340
  • 2
  • 13

3 Answers3

4
let words = ["23412334","&234@fwv","Kofi", "Abena", "Peter", "Kweku", "Akosua"]

func sortedNumbersLast(words: [String]) -> [String] {
    var startsWithDigit     = [String]()
    var startsWithCharacter = [String]()
    var startsWithSymbol    = [String]()


    for word in words {
        if let first = word.characters.first {
            if first >= "0" && first <= "9" {
                startsWithDigit.append(word)
            }
            else {
                if(!(first >= "a" && first <= "z") && !(first >= "A" && first <= "Z") ){
                    startsWithSymbol.append(word)
                }else{
                    startsWithCharacter.append(word)
                }

            }
        }
    }
    return startsWithCharacter.sorted(by: <) + startsWithDigit.sorted(by: <) + startsWithSymbol.sorted()
}
print(sortedNumbersLast(words: words))

["Abena", "Akosua", "Kofi", "Kweku", "Peter", "23412334", "&234@fwv"]

Modified Answer

Im Batman
  • 1,842
  • 1
  • 31
  • 48
  • Sorry friend..... can you pls tell me....if I use dictionary array, how to do it like { PhoneNumbers = 5554787672; fullName = "Daniel Higgins"; } – joel prithivi Jul 24 '17 at 11:57
3

The "correct" way to do it is to define your own sort comparator and use sorted(by:) which sorts using your comparator. In the example below, I define a "priority" based on the category of the first character in the string, and then a two level sort based first on priority and then on the normal string ordering

extension String
{
    enum Priority
    {
        case normal
        case low
        case lowest
        static let lowPriority: Set<Character> = Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"])
        static let lowestPriority: Set<Character> = Set(["&"])

        static func < (a: Priority, b: Priority) -> Bool
        {
            switch (a, b)
            {
            case (.normal, .low), (.normal, .lowest), (.low, .lowest):
                return true
            default:
                return false
            }
        }
    }

    var priority: Priority
    {
        let first = self.characters.first! // fatal error for empty strings
        if Priority.lowestPriority.contains(first)
        {
            return .lowest
        }
        else if Priority.lowPriority.contains(first)
        {
            return .low
        }
        return .normal
    }
}

let students: Set = ["23412334","&234@fwv","Kofi", "Abena", "Peter", "Kweku", "Akosua"]

let sortedStudents = students.sorted {
    (a, b) -> Bool in
    if a.priority < b.priority
    {
        return true
    }
    else if b.priority < a.priority
    {
        return false
    }
    return a < b
}
print(sortedStudents)

There may be bugs still, but it works for your test case.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Friend..... please help me.... can you pls tell me....if I use dictionary array, how to do it like { PhoneNumbers = 5554787672; fullName = "Daniel Higgins"; } – joel prithivi Jul 24 '17 at 12:34
  • @joelprithivi It's probably best to ask that as another question. – JeremyP Jul 24 '17 at 13:09
0

Follow these steps

  • First sort the array as you have sorted.

  • Remove the strings starting with special characters and save it in another array.

  • Remove the strings starting with numerics and save it in another array.
  • Append the numeric array to original array.
  • Append the special character array to original array.
Faisal
  • 162
  • 1
  • 12