pierrederome
2008-May-07 09:29 UTC
Very easy "sort key" question (but I can''t figure it out)
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David A. Black
2008-May-07 10:15 UTC
Re: Very easy "sort key" question (but I can''t figure it out)
Hi -- On Wed, 7 May 2008, pierrederome wrote:> > 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,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 for details and updates! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Bush
2008-May-07 10:16 UTC
Re: Very easy "sort key" question (but I can''t figure it out
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David A. Black
2008-May-07 10:31 UTC
Re: Very easy "sort key" question (but I can''t figure it out)
Hi -- On Wed, 7 May 2008, David A. Black wrote:> > Hi -- > > On Wed, 7 May 2008, pierrederome wrote: > >> >> 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, > > 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.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 for details and updates! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
pierrederome
2008-May-07 11:01 UTC
Re: Very easy "sort key" question (but I can''t figure it out)
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... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-May-07 11:39 UTC
Re: Very easy "sort key" question (but I can''t figure it out)
On May 7, 2008, at 5:29 AM, pierrederome wrote:> 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, > > PierreOn 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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
pierrederome
2008-May-07 18:30 UTC
Re: Very easy "sort key" question (but I can''t figure it out)
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> > Based on YOUR answer above, allow me to restate your question in a way > that would have been MUCH more clear:--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---