Hi guys,
I'm testing iPhone development right now and I'm not yet an Objective-C freak. In my code I have a NSArray full of german highway identifiers. They all start with a single letter like "A" or "B" and continue with a number. So I have plenty of strings like "A1", "A3" or "A10".
To get them in order I want them to be sorted in a natural way so "A2" follows "A1" and so on. But Objective-C of course sorts them how a computer would do it, so first comes "A1" and then "A10" and "A11" instead of "A2".
I've found a ruby solution with which I'm far more familiar, but it's very hard for me to translate it into Objective-C. It basically turns the string into an Array and then makes use of Regex functions to figure out wheter the current character is a Number or not:
Code:
module Enumerable
def sensible_sort
sort_by { |key| key.split(/(\d+)/).map { |v| v =~ /\d/ ? v.to_i : v } }
end
end
If maybe someone could help me turning this code into a Objective-C compare-function this would be absolutely awesome!
Thanks in Advance
canbuffi