Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Sort pseudo-lists

Reply
Thread Tools

Sort pseudo-lists

 
 
pete boardman
Guest
Posts: n/a
 
      10-10-2005
Say I've got a string like this:

"{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4, 2.2}, {1.4, 2.2, 2.1, 4.2}}"

- imagine it's aspiring to be a list of sublists, with 4 reals in
each list. I want to produce a sorted version:

(1.4 2.2 2.1 4.2)
(2.1 3.14 2.4 2.2)
(3.1 1.3 2.5 2.1)

What would be a good way to do this?

thanks

Pete



 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      10-10-2005
On Oct 10, 2005, at 4:46 PM, pete boardman wrote:

> Say I've got a string like this:
>
> "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4, 2.2}, {1.4, 2.2, 2.1, 4.2}}"
>
> - imagine it's aspiring to be a list of sublists, with 4 reals in
> each list. I want to produce a sorted version:
>
> (1.4 2.2 2.1 4.2)
> (2.1 3.14 2.4 2.2)
> (3.1 1.3 2.5 2.1)
>
> What would be a good way to do this?


Well, if you can count on the data format, the following is pretty
simple:

irb(main):001:0> arr = eval "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4,
2.2}, {1.4, 2.2, 2.1, 4.2}}".tr("{}", "[]")
=> [[3.1, 1.3, 2.5, 2.1], [2.1, 3.1, 2.4, 2.2], [1.4, 2.2, 2.1, 4.2]]
irb(main):002:0>
irb(main):003:0* arr.sort { |a, b| a.first <=> b.first }
=> [[1.4, 2.2, 2.1, 4.2], [2.1, 3.1, 2.4, 2.2], [3.1, 1.3, 2.5, 2.1]]

Hope that helps.

James Edward Gray II



 
Reply With Quote
 
 
 
 
Ara.T.Howard
Guest
Posts: n/a
 
      10-10-2005
On Tue, 11 Oct 2005, pete boardman wrote:

> Say I've got a string like this:
>
> "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4, 2.2}, {1.4, 2.2, 2.1, 4.2}}"
>
> - imagine it's aspiring to be a list of sublists, with 4 reals in each list.
> I want to produce a sorted version:
>
> (1.4 2.2 2.1 4.2)
> (2.1 3.14 2.4 2.2)
> (3.1 1.3 2.5 2.1)
>
> What would be a good way to do this?


harp:~ > cat a.rb
buf = "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4, 2.2}, {1.4, 2.2, 2.1, 4.2}}"
buf.gsub! %r/ { /x, ' [ '
buf.gsub! %r/ } /x, ' ] '

require 'yaml'
list = YAML::load(buf).map{|s| s.sort}.sort

p list

harp:~ > ruby a.rb
[[1.3, 2.1, 2.5, 3.1], [1.4, 2.1, 2.2, 4.2], [2.1, 2.2, 2.4, 3.1]]


-a
--
================================================== =============================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
================================================== =============================



 
Reply With Quote
 
pete boardman
Guest
Posts: n/a
 
      10-11-2005
On 10 Oct 2005, at 22:53, James Edward Gray II wrote:

> arr = eval "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4, 2.2}, {1.4, 2.2,
> 2.1, 4.2}}".tr("{}", "[]")
> arr.sort { |a, b| a.first <=> b.first }


Thanks, James - simple, elegant, and nearly perfect! Is it easy to
sort the array of arrays by their second, third, and fourth entries
as well:

ie such that

[[2.1, 2.2, 9.5, 2.1], [2.1, 2.2, 2.4, 2.2], [2.1, 2.2, 2.1, 4.2]]

sorts to

[[2.1, 2.2, 2.1, 4.2],[2.1, 2.2, 2.4, 2.2],[2.1, 2.2, 9.5, 2.1]]

rather than

[[2.1, 2.2, 9.5, 2.1], [2.1, 2.2, 2.4, 2.2], [2.1, 2.2, 2.1, 4.2]]

thanks

Pete


 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      10-11-2005
pete boardman <> wrote:
> On 10 Oct 2005, at 22:53, James Edward Gray II wrote:
>
> > arr = eval "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4, 2.2}, {1.4, 2.2,
> > 2.1, 4.2}}".tr("{}", "[]")
> > arr.sort { |a, b| a.first <=> b.first }

>
> Thanks, James - simple, elegant, and nearly perfect! Is it easy to
> sort the array of arrays by their second, third, and fourth entries
> as well:


That's actually the default behaviour, so you can just say arr.sort

martin
 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      10-11-2005

pete boardman wrote:
> Is it easy to
> sort the array of arrays by their second, third, and fourth entries
> as well:
>
> ie such that
>
> [[2.1, 2.2, 9.5, 2.1], [2.1, 2.2, 2.4, 2.2], [2.1, 2.2, 2.1, 4.2]]
>
> sorts to
>
> [[2.1, 2.2, 2.1, 4.2],[2.1, 2.2, 2.4, 2.2],[2.1, 2.2, 9.5, 2.1]]
>
> rather than
>
> [[2.1, 2.2, 9.5, 2.1], [2.1, 2.2, 2.4, 2.2], [2.1, 2.2, 2.1, 4.2]]
>
> thanks
>
> Pete


a = "{{2.1, 2.2, 9.5, 2.1}, {2.1, 2.2, 2.4, 2.2}, {2.1, 2.2, 2.1,
4.2}}"\
[2..-3].split( '}, {' ).map{|x| x.split( /,\s+/ ).map{|s| s.to_f } }
p a.sort

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      10-11-2005

James Edward Gray II wrote:

> irb(main):001:0> arr = eval "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4,
> 2.2}, {1.4, 2.2, 2.1, 4.2}}".tr("{}", "[]")
> => [[3.1, 1.3, 2.5, 2.1], [2.1, 3.1, 2.4, 2.2], [1.4, 2.2, 2.1, 4.2]]
> irb(main):002:0>
> irb(main):003:0* arr.sort { |a, b| a.first <=> b.first }
> => [[1.4, 2.2, 2.1, 4.2], [2.1, 3.1, 2.4, 2.2], [3.1, 1.3, 2.5, 2.1]]


arr.sort_by{|x| x.first }

 
Reply With Quote
 
pete boardman
Guest
Posts: n/a
 
      10-11-2005
Thanks for these answers!

Pete


 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      10-11-2005
Hi --

On Tue, 11 Oct 2005, William James wrote:

>
> James Edward Gray II wrote:
>
>> irb(main):001:0> arr = eval "{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4,
>> 2.2}, {1.4, 2.2, 2.1, 4.2}}".tr("{}", "[]")
>> => [[3.1, 1.3, 2.5, 2.1], [2.1, 3.1, 2.4, 2.2], [1.4, 2.2, 2.1, 4.2]]
>> irb(main):002:0>
>> irb(main):003:0* arr.sort { |a, b| a.first <=> b.first }
>> => [[1.4, 2.2, 2.1, 4.2], [2.1, 3.1, 2.4, 2.2], [3.1, 1.3, 2.5, 2.1]]

>
> arr.sort_by{|x| x.first }


Better yet:

arr.sort

which will handle tie-breaks automatically.


David

--
David A. Black



 
Reply With Quote
 
pete boardman
Guest
Posts: n/a
 
      10-11-2005
One more thing - I forgot that I need to get the result back to a
string... So (thanks to your help andn Ruby's 'eval') I can go from
this:

"{{3.1, 1.3, 2.5, 2.1}, {2.1, 3.1, 2.4, 2.2}, {1.4, 2.2, 2.1, 4.2}}"

to this:

[[1.4, 2.2, 2.1, 4.2], [2.1, 3.1, 2.4, 2.2], [3.1, 1.3, 2.5, 2.1]]

but I can't persuade the 'p' method to turn this back into a string:

"[[1.4, 2.2, 2.1, 4.2], [2.1, 3.1, 2.4, 2.2], [3.1, 1.3, 2.5, 2.1]]"

(and I should be able to go from here to :

"{{1.4, 2.2, 2.1, 4.2}, {2.1, 3.1, 2.4, 2.2}, {3.1, 1.3, 2.5, 2.1}}"

on my own


Pete






 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: When will Thunderbird support sort in place (in context sort)? Ron Natalie Firefox 0 02-02-2006 04:38 AM
The Colourised Bewitched -- sort of OK....... sort of! anthony DVD Video 26 06-28-2005 04:39 AM
xsl:sort lang="es" modern vs. tradidional Spanish sort order nobody XML 0 06-01-2004 06:25 AM
What is faster? C++ vector sort or sort in database JerryJ C++ 11 04-28-2004 10:23 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57