Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 05-08-2009, 04:44 PM   #1 (permalink)
canbuffi
New Member
 
Join Date: Mar 2009
Posts: 3
canbuffi is on a distinguished road
Default Sorting an Array with strings like "A1", "A2","A10"...

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
canbuffi is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 175,637
Threads: 94,101
Posts: 402,822
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jessicagx69
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:26 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.