Sort alphanumeric array, consecutive numbers should reside at last

后端 未结 2 1611
我在风中等你
我在风中等你 2021-01-27 13:03

I want to sort an alphanumeric, but numbers should come at end instead of beginning. for instance

let array = [\"1\", \"a\", \"b\", \"z\", \"3\", \"!\"]
let sor         


        
相关标签:
2条回答
  • 2021-01-27 13:30

    Here's a solution that works for your case :

    The expected result is :

    • letters first ;
    • then numbers ;
    • then others ;

    Here's a way to do it.

    // Works with both arrays
    var array = ["1", "b", "a", "z", "3", "!"]
    //var array = ["1", "b", "A", "Z", "3", "!"]
    
    func isLetter(char: String) -> Bool {
        return ("a"..."z").contains(char) || ("A"..."Z").contains(char)
    }
    
    func isNumber(char: String) -> Bool {
        return Int(char) != nil
    }
    
    let letters = array.filter(isLetter).sort{$0.lowercaseString < $1.lowercaseString}
    let numbers = array.filter(isNumber).sort{Int($0) < Int($1)}
    let others = Array(Set(array).subtract(letters).subtract(numbers)).sort{$0 < $1}
    
    let sortedArray = letters + numbers + others
    

    The first array would be

    ["a", "b", "z", "1", "3", "!"]
    

    The second would be

    ["A", "b", "Z", "1", "3", "!"]
    

    It does what you want. Include unit tests and wrap that inside a method and you're good to go. Buuuut, it's not "clean" at all.

    0 讨论(0)
  • 2021-01-27 13:42

    Assume that you want to receive "numbers should come at end instead of beginning", without watching your example with unexpected "!" sorting. Then you can do this:

    let array = ["1", "a", "b", "z", "3", "!"]
    let sortedArray = array.sort { (firstObject, secondObject) -> Bool in
        let firstIsNumber = firstObject.rangeOfCharacterFromSet(NSCharacterSet.decimalDigitCharacterSet())?.count > 0
        let secondIsNumber = secondObject.rangeOfCharacterFromSet(NSCharacterSet.decimalDigitCharacterSet())?.count > 0
        if firstIsNumber != secondIsNumber {return secondIsNumber}
        return firstObject < secondObject
    }
    print(sortedArray)
    //["!", "a", "b", "z", "1", "3"]
    
    0 讨论(0)
提交回复
热议问题