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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-08-2009, 05:44 PM   #1 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 3
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
Old 05-08-2009, 08:26 PM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Check out the options for string comparison in the NSString and CFString class descriptions. Especially the kCFCompareNumerically comparator.

jow
FlyingDiver is offline   Reply With Quote
Reply

Bookmarks

Tags
array, number, sorting, string

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 260
17 members and 243 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, Grinarn, HemiMG, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:49 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0