[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

tricky sort for happy visitors of Paris

Josselin

12/20/2006 3:03:00 PM

all happy visitors of Paris know about the 'arrondissement', an
administrative division of the city... 20 divisions

selecting them from a city table I can map them as :

['Paris 1' , 'Paris 10' , 'Paris 11' , ..... 'Paris 19' , 'Paris 2' ,
'Paris 20' , 'Paris 3' , .... 'Paris 9' ]

is there any way to sort this array and get :

['Paris 1' , 'Paris 2' , 'Paris 3' , .... 'Paris 9' , 'Paris 10' ,
'Paris 11' , ..... 'Paris 19' , 'Paris 20' ]

which seems betetr in a list... ;-))

thanks for your light

joss

17 Answers

dblack

12/20/2006 3:15:00 PM

0

Rob Biedenharn

12/20/2006 3:18:00 PM

0


On Dec 20, 2006, at 10:05 AM, Josselin wrote:

> all happy visitors of Paris know about the 'arrondissement', an
> administrative division of the city... 20 divisions
>
> selecting them from a city table I can map them as :
>
> ['Paris 1' , 'Paris 10' , 'Paris 11' , ..... 'Paris 19' , 'Paris
> 2' , 'Paris 20' , 'Paris 3' , .... 'Paris 9' ]
>> paris = (1..20).map {|i| "Paris #{i}"}.sort
=> ["Paris 1", "Paris 10", "Paris 11", "Paris 12", "Paris 13", "Paris
14", "Paris 15", "Paris 16", "Paris 17", "Paris 18", "Paris 19",
"Paris 2", "Paris 20", "Paris 3", "Paris 4", "Paris 5", "Paris 6",
"Paris 7", "Paris 8", "Paris 9"]

> is there any way to sort this array and get :
>
> ['Paris 1' , 'Paris 2' , 'Paris 3' , .... 'Paris 9' , 'Paris 10' ,
> 'Paris 11' , ..... 'Paris 19' , 'Paris 20' ]
>
> which seems betetr in a list... ;-))
>
> thanks for your light
>
> joss

>> paris.sort_by { |division| division.match(/(\d+)/)[1].to_i }
=> ["Paris 1", "Paris 2", "Paris 3", "Paris 4", "Paris 5", "Paris 6",
"Paris 7", "Paris 8", "Paris 9", "Paris 10", "Paris 11", "Paris 12",
"Paris 13", "Paris 14", "Paris 15", "Paris 16", "Paris 17", "Paris
18", "Paris 19", "Paris 20"]

if you have other cities or more than one embedded number, this is
likely too simple, but it works for your example.

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Nicolas Desprès

12/20/2006 3:18:00 PM

0

On 12/20/06, Josselin <josselin@wanadoo.fr> wrote:
> all happy visitors of Paris know about the 'arrondissement', an
> administrative division of the city... 20 divisions
>
> selecting them from a city table I can map them as :
>
> ['Paris 1' , 'Paris 10' , 'Paris 11' , ..... 'Paris 19' , 'Paris 2' ,
> 'Paris 20' , 'Paris 3' , .... 'Paris 9' ]
>
> is there any way to sort this array and get :
>
> ['Paris 1' , 'Paris 2' , 'Paris 3' , .... 'Paris 9' , 'Paris 10' ,
> 'Paris 11' , ..... 'Paris 19' , 'Paris 20' ]
>

Somethings like (I've not tested the code):

['Paris 1' , 'Paris 2' , 'Paris 3' , .... 'Paris 9' , 'Paris 10' ,
'Paris 11' , ..... 'Paris 19' , 'Paris 20' ].sort do |a, b|
re = /\s(\d+)$/
re.match(a)
ai = $1.to_i
re.match(b)
bi = $1.to_i
ai <=> bi
end

Cheers

--
Nicolas Desprès

Farrel Lifson

12/20/2006 3:19:00 PM

0

On 20/12/06, Josselin <josselin@wanadoo.fr> wrote:
> all happy visitors of Paris know about the 'arrondissement', an
> administrative division of the city... 20 divisions
>
> selecting them from a city table I can map them as :
>
> ['Paris 1' , 'Paris 10' , 'Paris 11' , ..... 'Paris 19' , 'Paris 2' ,
> 'Paris 20' , 'Paris 3' , .... 'Paris 9' ]
>
> is there any way to sort this array and get :
>
> ['Paris 1' , 'Paris 2' , 'Paris 3' , .... 'Paris 9' , 'Paris 10' ,
> 'Paris 11' , ..... 'Paris 19' , 'Paris 20' ]
>
> which seems betetr in a list... ;-))
>
> thanks for your light
>
> joss
>
>
>

["Paris 1","Paris 10","Paris 2"].sort_by{|a| a.split[1].to_i}

Farrel

Jamey Cribbs

12/20/2006 3:20:00 PM

0

Josselin wrote:
> all happy visitors of Paris know about the 'arrondissement', an
> administrative division of the city... 20 divisions
>
> selecting them from a city table I can map them as :
>
> ['Paris 1' , 'Paris 10' , 'Paris 11' , ..... 'Paris 19' , 'Paris 2'
> , 'Paris 20' , 'Paris 3' , .... 'Paris 9' ]
>
> is there any way to sort this array and get :
>
> ['Paris 1' , 'Paris 2' , 'Paris 3' , .... 'Paris 9' , 'Paris 10' ,
> 'Paris 11' , ..... 'Paris 19' , 'Paris 20' ]

arrondissement_arr.sort_by { |a| a.split.last.to_i }


Jamey

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.

Uma Geller

12/20/2006 3:21:00 PM

0

> which seems betetr in a list... ;-))


arrondissements = ["Paris 1", "Paris 2","Paris 4", "Paris 3"]
sorted_array = arrondissements.sort_by {|e| e.split(" ")[1].to_i }

---
Uma Geller
http://umageller.wor...

Peter Hickman

12/20/2006 3:26:00 PM

0

This is just begging for a natural sort order method for the array
class. Has anyone created one yet?


Uma Geller

12/20/2006 3:29:00 PM

0

> arrondissement_arr.sort_by { |a| a.split.last.to_i }

your arys are better than myne ! :-)

thanks for the tip


---
Uma Geller
http://umageller.wor...

Johan Holmberg

12/20/2006 9:32:00 PM

0

On 12/20/06, Josselin <josselin@wanadoo.fr> wrote:
[...]
>
> ['Paris 1' , 'Paris 10' , 'Paris 11' , ..... 'Paris 19' , 'Paris 2' ,
> 'Paris 20' , 'Paris 3' , .... 'Paris 9' ]
>
> is there any way to sort this array and get :
>
> ['Paris 1' , 'Paris 2' , 'Paris 3' , .... 'Paris 9' , 'Paris 10' ,
> 'Paris 11' , ..... 'Paris 19' , 'Paris 20' ]
>
> which seems betetr in a list... ;-))
>

I have had a similar problem several times (but not with Paris :), and
I wrote a general utility function like this:

def sort_numbers_numerically(arr)
arr.sort_by do |str|
i = 0
str.split(/(\d+)/).map do |part|
i += 1
i % 2 == 0 ? part.to_i : part
end
end
end

It is of course very similar to the previously proposed solutions, but
is more general in that it sorts strings with several numbers in them,
treating each number "numerically" (one extreme example could be
IP-numbers).

/johan

Josselin

12/20/2006 10:14:00 PM

0

On 2006-12-20 16:03:28 +0100, Josselin <josselin@wanadoo.fr> said:

> all happy visitors of Paris know about the 'arrondissement', an
> administrative division of the city... 20 divisions
>
> selecting them from a city table I can map them as :
>
> ['Paris 1' , 'Paris 10' , 'Paris 11' , ..... 'Paris 19' , 'Paris 2' ,
> 'Paris 20' , 'Paris 3' , .... 'Paris 9' ]
>
> is there any way to sort this array and get :
>
> ['Paris 1' , 'Paris 2' , 'Paris 3' , .... 'Paris 9' , 'Paris 10' ,
> 'Paris 11' , ..... 'Paris 19' , 'Paris 20' ]
>
> which seems betetr in a list... ;-))
>
> thanks for your light
>
> joss

Thanks to all of you.... cannot invite you for a Xmas drink on the
Champs-Elysees but cheers...
(whatever I am not living in Paris.... but in the Celtic land.... the
French Far West....