Forum: Ruby Re: neuroimage software - scientific computing and visualiza

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Daniel S. (Guest)
on 2006-02-03 03:19
(Received via mailing list)
> So are you saying that if you have an array like:
>   a = ['d', 'b', 'a', 'c', 'e']
> You would like to get something like:
>   a.sort_index #=> [2, 1, 3, 0, 4]
> If that's the case, then I'm sure it's doable.  No built-in
> method that I'm
> aware of, but you could easily create one.

For example:

class Array
  def sort_index
    (0...size).sort_by {|i| self[i]}

%w{d b a c e}.sort_index
=> [2, 1, 3, 0, 4]
This topic is locked and can not be replied to.