Afficher un message
Vieux 12/03/2008, 20h56   #8
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sort array by some text

Ma Fe wrote:
> well I dont know where in array i have .txt files but its sure that
> there is just one and i want to put it at first place of array...


> a.select {|i| i =~ /\..txt$/}


That doesn't work.

> b = a.partition {|i| i =~ /\.txt$/}


Neither does that.

This is faster than all the solutions posted so far:


a = ['one.jpg', 'two.kzv', 'a.txt', 'a.az']
new_a = [1]

for elmt in a
if elmt[-3] == ?t
new_a[0] = elmt
else
new_a << elmt
end
end

--output:--
["a.txt", "one.jpg", "two.kzv", "a.az"]
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
 
Page generated in 0,05827 seconds with 9 queries