I have a function I was using in Xcode 8 Beta, which sorted an array alphabetically then numerically.
The function I used was:
func sortArray(arrayToSort: [String])->[String]{
let sortedArray = arrayToSort.sorted {
(first, second) in
first.compare(second, options: .numericSearch) == ComparisonResult.orderedAscending
}
print(sortedArray)
return sortedArray
}
However this doesnt work anymore. Does anyone had any idea how I can do this in Swift 3?
There is a function sort() through which you can sort your array like this
var arrSort = ["Rajat", "Test", "Sort", "Array","2","6"]
arrSort.sort()
print(arrSort)
After some further digging I found an answer:
let myArray = ["Step 6", "Step 12", "Step 10"]
let sortedArray = myArray.sorted {
$0.compare($1, options: .numeric) == .orderedAscending
}
print(sortedArray) // ["Step 6", "Step 10", "Step 12"]
This was based on Martin R, and Duncan C's posts.
Thank You! ^_______^
You can try this example as below:
func sortArray(arrayToSort: [String])->[String] {
let sortedArray = arrayToSort.sorted(by:) {
(first, second) in
first.compare(second, options: .numeric) == ComparisonResult.orderedAscending
}
print(sortedArray)
return sortedArray
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.