Reputation: 2317
Consider this array:
let files = ["file2.tif", "file33.jpg", "file11.jpg"]
I would like to sort this array so the result would be
sortedFiles = ["file2.tif", "file11.jpg", "file33.jpg"]
There are two problems here that I don't know how to approach:
1) how to ignore the file extension when sorting
2) how to make sure that "file2" is put in front of "file11"
Thank you so much for your insights
Upvotes: 1
Views: 503
Reputation: 285082
An option is to cast the strings to NSString
and use Foundation methods deletingPathExtension
and compare:options:.numeric
let files = ["file2.tif", "file33.jpg", "file11.jpg"]
let sortedFiles = files.sorted(by: {
return ($0 as NSString).deletingPathExtension.compare(($1 as NSString).deletingPathExtension, options: .numeric) == .orderedAscending
})
Upvotes: 2