From: Stefan Rusterholz Date: 2008-10-31T06:33:10+09:00 Subject: Re: how to sort this array Li Chen wrote: > Hi, > > I have an array containing files names. How do I sort them so that I can > get the expected results? > > Thanks, > > Li > > ############# > files=[ > "c:/ruby/self/2004/20.txt", > "c:/ruby/self/2004/3.txt", > "c:/ruby/self/2004/2.txt", > "c:/ruby/self/2004/10.txt", > "c:/ruby/self/2004/1.txt" > ] > > expected results: > [ > "c:/ruby/self/2004/1.txt", > "c:/ruby/self/2004/2.txt", > "c:/ruby/self/2004/3.txt", > "c:/ruby/self/2004/10.txt", > ] > "c:/ruby/self/2004/20.txt", You get that result because in "2" <=> "10" the comparison is byte by byte and then "2" is > "1", so it aborts there with "2" being > than "10". What you want is called natural sorting. Googling for natsort and ruby you should get a few results. Regards Stefan -- Posted via http://www.ruby-forum.com/.