From: James Edward Gray II Date: 2005-10-11T06:53:25+09:00 Subject: Re: Sort pseudo-lists 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