PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > sort array by some text
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
sort array by some text

Réponse
 
LinkBack Outils de la discussion
Vieux 12/03/2008, 06h02   #1
Ma Fe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sort array by some text

Hi, i have array with file names
a = ['one.jpg', 'two.txt', 'three.pdf']
how can i sort it that always first value will be *.txt file ?

Thanks, fryt
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 06h25   #2
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sort array by some text

On Wed, Mar 12, 2008 at 12:02 AM, Ma Fe <frytaz@gmail.com> wrote:
> Hi, i have array with file names
> a = ['one.jpg', 'two.txt', 'three.pdf']
> how can i sort it that always first value will be *.txt file ?
>
> Thanks, fryt
> --
> Posted via http://www.ruby-forum.com/.
>
>


a = ['one.jpg', 'two.txt', 'three.pdf']
#assume you want reverse sort?
a.sort_by {|i| i.scan /\..*?$/}.reverse

You understand, of course, this won't give you .txt as a first hit if
you have a .zip. It simply reverse sorts by the letters following a
dot.

If you just want .txt files, maybe use #select instead...

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

Or you could use #partition if you don't care about the order of other
attributes, but want to hold on to the entire array...

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

Todd

  Réponse avec citation
Vieux 12/03/2008, 06h34   #3
Ma Fe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sort array by some text

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...
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 06h53   #4
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sort array by some text

On Wed, Mar 12, 2008 at 12:34 AM, Ma Fe <frytaz@gmail.com> 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...
> --
>


Try using the #partition method (my last example) then. It gives you
all .txt files at the beginning of the array. I made a mistake
though. There should only be one dot, not two for that (same thing if
you use #select)...

a = ['one.jpg', 'two.txt', 'three.pdf', 'four.txt']
a_new = a.partition {|i| i =~ /\.txt$/}
p a_new

Todd

  Réponse avec citation
Vieux 12/03/2008, 11h34   #5
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sort array by some text

2008/3/12, Todd Benson <caduceass@gmail.com>:
> On Wed, Mar 12, 2008 at 12:34 AM, Ma Fe <frytaz@gmail.com> 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...

>
> Try using the #partition method (my last example) then. It gives you
> all .txt files at the beginning of the array. I made a mistake
> though. There should only be one dot, not two for that (same thing if
> you use #select)...
>
> a = ['one.jpg', 'two.txt', 'three.pdf', 'four.txt']
> a_new = a.partition {|i| i =~ /\.txt$/}
> p a_new


For the fun of it: here's a solution with #inject:

irb(main):009:0> a = ['one.jpg', 'two.txt', 'three.pdf']
=> ["one.jpg", "two.txt", "three.pdf"]
irb(main):010:0> a.inject([[],[]]) {|(tx,ot),x| (/\.txt$/ =~ x ? tx :
ot) << x;[tx,ot]}.flatten
=> ["two.txt", "one.jpg", "three.pdf"]

There's even a version with *two* #injects:

irb(main):011:0> a.inject([[],[]]) {|(tx,ot),x| (/\.txt$/ =~ x ? tx :
ot) << x;[tx,ot]}.inject {|a,b| a.concat b}
=> ["two.txt", "one.jpg", "three.pdf"]

It's been a while so I *had* to do it. Once in a while I need my
#injection. :-))

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 12/03/2008, 15h43   #6
Rodrigo Bermejo
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sort array by some text


a = ['one.jpg', 'two.txt', 'three.pdf','a.txt', 'a.az' , 'a.z']
a_new=a.sort_by do |x|
if x =~ /\.txt$/
0
else
1
end
end
p a_new
--> ["a.txt", "two.txt", "one.jpg", "three.pdf", "a.az", "a.z"]

the sort_by block can be improved surely.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 16h20   #7
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sort array by some text

2008/3/12, Rodrigo Bermejo <rodrigo.bermejo@ps.ge.com>:
>
> a = ['one.jpg', 'two.txt', 'three.pdf','a.txt', 'a.az' , 'a.z']
> a_new=a.sort_by do |x|
> if x =~ /\.txt$/
> 0
> else
> 1
> end
> end
> p a_new
> --> ["a.txt", "two.txt", "one.jpg", "three.pdf", "a.az", "a.z"]
>
> the sort_by block can be improved surely.


Here's one way: sorting with priorities:

irb(main):006:0> a = ['one.jpg', 'two.txt', 'three.pdf','a.txt', 'a.az' , 'a.z']
=> ["one.jpg", "two.txt", "three.pdf", "a.txt", "a.az", "a.z"]
irb(main):007:0> a.sort_by {|s| [/\.txt$/=~s ? 0 : 1, s]}
=> ["a.txt", "two.txt", "a.az", "a.z", "one.jpg", "three.pdf"]

;-)

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 12/03/2008, 19h56   #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
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h58.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17739 seconds with 16 queries