On Mar 12, 1:56 pm, 7stud -- <bbxx789_0... / yahoo.com> 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...

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


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