If you're getting this array from an external source which doesn't provide the numerical information along with the strings, you'll have to extract the numbers from them yourself. Of course if you had the numerical information to begin with at some point, you should've hung onto it as @matt says.
One way of getting the numerical information back out of your strings would be using flatMap on your array, followed by using stringByTrimmingCharactersInSet in order to trim away non-numerical characters from each end of the string. You can then attempt to create a double out of it in order to create a new array of (String, Double) tuples, and using flatMap to filter out the nil results. For example:
let strings = ["foobar", "Hello (45.5 km)", "George St. (39.5 km)", "Salut (20.45 km)", "Bonjour (30 km)", "Hi (35 km)"]
let stringsWithNumbers = strings.flatMap {str in
// trim away the non numerical characters, and attempt to create a double from the result
Double(str.stringByTrimmingCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)).map {(string: str, number: $0)}
}
print(stringsWithNumbers) // [("Hello (45.5 km)", 45.5), ("George St. (39.5 km)", 39.5), ("Salut (20.45 km)", 20.449999999999999), ("Bonjour (30 km)", 30.0), ("Hi (35 km)", 35.0)]
Note that this won't work with strings with multiple numbers in (although I can't think of how you'd want to handle that situation anyway).
If you know that you're distances are always contained in rounded brackets, you could add some extra code in order to allow numbers outside the brackets, by first trimming the string down to the brackets, then trimming to the number. For example:
let strings = ["foobar", "5th Avenue (3 km)", "Hello (45.5 km)", "George St. (39.5 km)", "Salut (20.45 km)", "Bonjour (30 km)", "Hi (35 km)"]
let stringsWithNumbers:[(string:String, number:Double)] = strings.flatMap{str in
// the substring of the brackets
let brackets = str.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "()").invertedSet)
// the number within the brackets
let num = brackets.stringByTrimmingCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)
// attempt to create a double from the result
return Double(num).map {(string: str, number: $0)}
}
print(stringsWithNumbers) // [("5th Avenue (3 km)", 3.0), ("Hello (45.5 km)", 45.5), ("George St. (39.5 km)", 39.5), ("Salut (20.45 km)", 20.449999999999999), ("Bonjour (30 km)", 30.0), ("Hi (35 km)", 35.0)]
You can then sort this array by using the sort function. For example:
let sorted = stringsWithNumbers.sort {$0.number < $1.number}.map{$0.string} // sort, then ditch the distance information
print(sorted) // ["5th Avenue (3 km)", "Salut (20.45 km)", "Bonjour (30 km)", "Hi (35 km)", "George St. (39.5 km)", "Hello (45.5 km)"]
If you wish to keep the tuples of strings with doubles, you can remove the map at the end.