# Sort array by some text

**URL:** <https://www.ruby-forum.com/t/sort-array-by-some-text/131650>\
**Category:** Ruby\
**Created:** [March 12, 2008, 6:02am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650 "2008-03-12T06:02:53Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 6:02am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/1 "2008-03-12T06:02:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 6:34am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/2 "2008-03-12T06:34:38Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 6:26am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/3 "2008-03-12T06:26:54Z")

</div>

On Wed, Mar 12, 2008 at 12:02 AM, Ma Fe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](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

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 6:54am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/4 "2008-03-12T06:54:27Z")

</div>

On Wed, Mar 12, 2008 at 12:34 AM, Ma Fe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 11:35am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/5 "2008-03-12T11:35:16Z")

</div>

2008/3/12, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/6 "2008-03-12T16:21:10Z")

</div>

2008/3/12, Rodrigo B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> → [“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

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/7 "2008-03-12T19:56:37Z")

</div>

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”]

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/8 "2008-03-12T15:43:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 9:42pm UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/9 "2008-03-12T21:42:28Z")

</div>

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…

Do you want to sort the array, or just make sure that element is the  
first one? If the former case, and speed is an issue, you can just swap  
the first element with it which should reduce time of execution. Of  
course, I have no empirical evidence of this, but thought that this  
question might reduce if that is the case.

Mac

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/10 "2008-03-12T20:50:58Z")

</div>

On Mar 12, 1:56 pm, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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…

> end  
> end
> 
> –output:–  
> [“a.txt”, “one.jpg”, “two.kzv”, “a.az”]

Iters = 99\_000  
a, new\_a = nil

t = Time.now  
Iters.times{  
a = %w(one.jpg two.kzv a.az b.mp3 c.png a.txt d.tif e.gif f.mp4  
g.pdf)  
new\_a = [1]  
for elmt in a  
if elmt[-4,4] == ‘.txt’  
new\_a[0] = elmt  
else  
new\_a \<\< elmt  
end  
end  
}  
p Time.now - t

t = Time.now  
Iters.times{  
a = %w(one.jpg two.kzv a.az b.mp3 c.png a.txt d.tif e.gif f.mp4  
g.pdf)  
1.upto(a.size-1){|i|  
if a[i][-4,4] == ‘.txt’  
a[0],a[i] = a[i],a[0]  
break  
end  
}  
}  
p Time.now - t

–output–  
4.422  
3.359

Lua:

when = os.clock()

for \_ = 1,99000 do  
a = {‘one.jpg’,‘two.uzv’,‘a.az’,‘b.mp3’,‘c.png’,‘a.txt’,  
‘d.tif’,‘e.gif’,‘f.mp4’,‘g.pdf’}  
for i = 2, #a do  
if string.sub( a[i], -4 ) == ‘.txt’ then  
a[1],a[i] = a[i],a[1]  
end  
end  
end

print( os.clock() - when )

–output–  
0.968

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/11 "2008-03-12T23:52:23Z")

</div>

I’ve seen many post lately, where seems the best solution is the faster  
one.

but remember, optimize until you need it…programmer time vs. cpu  
time…maintenance.

Use ruby, if there is a speed need that ruby does not cover, you already  
have a prototype to build a faster implementation.

the turtle vs. rabbit fable.

there’s always an open door.  
faster=`C_Program`

-r.

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 12, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/12 "2008-03-12T23:34:10Z")

</div>

On Wed, Mar 12, 2008 at 1:56 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Neither does that.

Yes, I said all this in the next post.

Todd

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 13, 2008, 12:18am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/13 "2008-03-13T00:18:16Z")

</div>

On Wed, Mar 12, 2008 at 6:13 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > if elmt[-3] == ?t  
> > one text file.  
> > puts a  
> > I reply to myself too often on this board. That won’t work either,  
> > because it won’t preserve the array contents.

Todd

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 13, 2008, 12:14am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/14 "2008-03-13T00:14:09Z")

</div>

On Wed, Mar 12, 2008 at 1:56 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> new_a << elmt
> 
> ```
> 
> end  
> end
> 
> –output:–  
> [“a.txt”, “one.jpg”, “two.kzv”, “a.az”]

Fast and somewhat of an eyesore. Doesn’t work if there’s more than  
one text file.

About the same speed and more rubyish…

a = [‘one.jpg’, ‘two.kzv’, ‘a.txt’, ‘a.az’]  
t = Time.now  
new\_a =   
for elmt in a  
(a.unshift elmt && a.pop) if elmt[-3] == ?t  
end  
puts a

cheers,  
Todd

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 13, 2008, 12:27am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/15 "2008-03-13T00:27:19Z")

</div>

Todd B. wrote:

> On Wed, Mar 12, 2008 at 1:56 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ```
> > new_a << elmt
> > 
> > ```
> > 
> > end  
> > end
> > 
> > –output:–  
> > [“a.txt”, “one.jpg”, “two.kzv”, “a.az”]
> 
> Fast and somewhat of an eyesore. Doesn’t work if there’s more than  
> one text file.

And since there isn’t…

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 13, 2008, 12:24am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/16 "2008-03-13T00:24:43Z")

</div>

On Wed, Mar 12, 2008 at 6:13 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> a = [‘one.jpg’, ‘two.kzv’, ‘a.txt’, ‘a.az’]  
> new\_a =   
> for elmt in a  
> (a.unshift elmt && a.pop) if elmt[-3] == ?t  
> change this ^^^^^^ line to “a.unshift a.delete(elem) if elmt[-3] == ?t”

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 13, 2008, 12:51am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/17 "2008-03-13T00:51:10Z")

</div>

On Wed, Mar 12, 2008 at 6:27 PM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> And since there isn’t…

Okay … works to spec.

---

<div class="post-metadata">

**Author:** ![fryt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7bcc69/32.png) [@fryt](https://www.ruby-forum.com/u/fryt)\
**Post date:** [March 13, 2008, 1:15am UTC](https://www.ruby-forum.com/t/sort-array-by-some-text/131650/18 "2008-03-13T01:15:52Z")

</div>

On Wed, Mar 12, 2008 at 6:50 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Okay … works to spec.

No it doesn’t, what if you tap on a “something.tgz” file on the end of  
the array. William had the right idea.

Todd
