# Very easy "sort key" question (but I can't figure it out)

**URL:** <https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916>\
**Category:** Rails\
**Created:** [May 7, 2008, 11:30am UTC](https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916 "2008-05-07T11:30:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierrederome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c67d28/32.png) [@pierrederome](https://www.ruby-forum.com/u/pierrederome)\
**Post date:** [May 7, 2008, 11:30am UTC](https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916/1 "2008-05-07T11:30:05Z")

</div>

hello,

I have an array, for instance:

a = [10, 2, 20, 14, 33]

I want to get:

order= [5, 3, 4, 1, 2]

I’m sure this can be done in one neat line… I only achieve ugly  
twisted code to do it… please enlighten me,

many thanks,

Pierre

---

<div class="post-metadata">

**Author:** ![pierrederome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c67d28/32.png) [@pierrederome](https://www.ruby-forum.com/u/pierrederome)\
**Post date:** [May 7, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916/2 "2008-05-07T12:16:33Z")

</div>

Hi –

On Wed, 7 May 2008, pierrederome wrote:

> I’m sure this can be done in one neat line… I only achieve ugly  
> twisted code to do it… please enlighten me,

a.sort.reverse would work in this particular case. Otherwise you can  
do:

new\_array = order.map {|i| a[i-1] }

The reason for the i - 1 thing is that your order goes from 1-5,  
whereas the indices of the array are 0-4. But I suspect the  
sort.reverse technique might be what you need anyway.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
INTRO TO RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![pierrederome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c67d28/32.png) [@pierrederome](https://www.ruby-forum.com/u/pierrederome)\
**Post date:** [May 7, 2008, 12:16pm UTC](https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916/3 "2008-05-07T12:16:46Z")

</div>

pierrederome wrote:

> I have an array, for instance:  
> a = [10, 2, 20, 14, 33]  
> I want to get:  
> order= [5, 3, 4, 1, 2]

(1…(a.length)).sort {|i, j| a[j-1] \<=\> a[i-1]}

Though short one-liners are not generally things to aim for. I have  
used Perl for almost 20 years and used to get a thrill out of finding  
concise ways of doing things, until looking back on them later and  
finding it would take me just as long each time to re-work out what was  
going on.

Splitting problems up into multiple lines and using intermediate  
variables that _mean_ something about the nature or use of the value  
aids readability and maintainability.

---

<div class="post-metadata">

**Author:** ![pierrederome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c67d28/32.png) [@pierrederome](https://www.ruby-forum.com/u/pierrederome)\
**Post date:** [May 7, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916/4 "2008-05-07T12:31:57Z")

</div>

Hi –

On Wed, 7 May 2008, David A. Black wrote:

> > a = [10, 2, 20, 14, 33]
> 
> new\_array = order.map {|i| a[i-1] }
> 
> The reason for the i - 1 thing is that your order goes from 1-5,  
> whereas the indices of the array are 0-4. But I suspect the  
> sort.reverse technique might be what you need anyway.

Based on Mark’s answer, I think I misunderstood your question.

So…

To derive the order array from a, you could do:

sorted = a.sort.reverse  
order = sorted.map {|e| a.index(e) + 1 }

or (a variant of Mark’s):

order = (1…a.length).sort\_by {|i| -a[i-1] }

I’m assuming that you’re always dealing with integers, and that values  
are unique.

David

–  
Rails training from David A. Black and Ruby Power and Light:  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
INTRO TO RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![pierrederome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c67d28/32.png) [@pierrederome](https://www.ruby-forum.com/u/pierrederome)\
**Post date:** [May 7, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916/5 "2008-05-07T13:02:42Z")

</div>

thanks a lot !

in the end I did something pretty close

b= []  
a.each\_with\_index { |v,i| b[i] = [v, i] }  
order=b.sort\_by { |o| o[0] }.map { |v| v[1] }.reverse

cause values were not unique indeed…

---

<div class="post-metadata">

**Author:** ![pierrederome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c67d28/32.png) [@pierrederome](https://www.ruby-forum.com/u/pierrederome)\
**Post date:** [May 7, 2008, 1:39pm UTC](https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916/6 "2008-05-07T13:39:58Z")

</div>

On May 7, 2008, at 5:29 AM, pierrederome wrote:

> I’m sure this can be done in one neat line… I only achieve ugly  
> twisted code to do it… please enlighten me,
> 
> many thanks,
> 
> Pierre

On May 7, 2008, at 7:01 AM, pierrederome wrote:

> thanks a lot !
> 
> in the end I did something pretty close
> 
> b= []  
> a.each\_with\_index { |v,i| b[i] = [v, i] }  
> order=b.sort\_by { |o| o[0] }.map { |v| v[1] }.reverse
> 
> cause values were not unique indeed…

Pierre,  
You may have solved your problem, but I’d be surprised if anyone else  
could possibly grasp what that problem really was. David said, “Based  
on Mark’s answer, I think I misunderstood your question.” I think it  
more accurate to say, “Based on your vague question, I’m not sure what  
answer you expect.”

Based on YOUR answer above, allow me to restate your question in a way  
that would have been MUCH more clear:

Given an array of integers (which are not necessarily  
unique), I need to construct the array that gives the  
1-based (rather than the natural 0-based) “place values”  
of those integers, but counting from the end of the  
original array.  
For example,  
a = [10, 2, 20, 14, 33]  
should produce the array:  
[5, 3, 4, 1, 2]  
because the last value, 33, would come in “5th place”,  
then 14 would come in “3rd place”, etc.

Then perhaps a bit of context as to WHY such a transformation is  
useful to you. It’s entirely possibly that knowing more about your  
problem would give you a solution that you might have never imagined.

Seriously, if David A. Black hadn’t already responded, I would have  
simple sent your original, vague problem to the bit-bucket.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pierrederome](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c67d28/32.png) [@pierrederome](https://www.ruby-forum.com/u/pierrederome)\
**Post date:** [May 7, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/very-easy-sort-key-question-but-i-cant-figure-it-out/136916/7 "2008-05-07T20:31:31Z")

</div>

indeed, next time, i’ll do my best to be more clear ! and of course,  
from a clear question, often comes a clear answer !

Mind you the answers were VERY useful to me…so tx again
