Javier Gomez Javier Gomez - 2 years ago 117
Swift Question

Sort array by second letter in swift 2.2

I want to sort the array by second letter of word. Some one has an idea? i will type a value and key, should return sorted list like this:
array ["hello", "bye", "how", etc"]
Sorted: hello, how, etc, bye

static func sort(keyValue: [(key:String, value:String)]) -> [(key:String, value:String)] {

let returnValue = keyValue( {$0 < $1}

return returnValue

Rob Rob
Answer Source

You can use substringFromIndex with the standard sort or sortInPlace functions:

let strings = ["hello", "bye", "how", "etc"]
let sortedStrings = strings.sort { $0.substringFromIndex($0.startIndex.advancedBy(1)) < $1.substringFromIndex($1.startIndex.advancedBy(1)) }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download