Sort order dilemma

I've got a array that I need sorted in a certain way and I'm struggling to figure out how to make it happen the way I want.

Basically the array needs to be sorted like this:

[A, B, C, AA, AB, AF ]

that is, a single alpha character before it sorts the next character. However, what I end up with when I sort is:

[A, AA, AB, AF, B, C]

How would I sort A to Z then AA-ZZ?

Wayne

array.sort_by {|s| [s.size, s]}

···

On Wed, Nov 2, 2011 at 11:21 AM, Wayne Brissette <waynefb@earthlink.net>wrote:

I've got a array that I need sorted in a certain way and I'm struggling to
figure out how to make it happen the way I want.

Basically the array needs to be sorted like this:

[A, B, C, AA, AB, AF ]

that is, a single alpha character before it sorts the next character.
However, what I end up with when I sort is:

[A, AA, AB, AF, B, C]

How would I sort A to Z then AA-ZZ?

Wayne

arr.sort do |x,y|
  if x.length < y.length
    return 1
  elsif x.length > y.length
    return -1
  else
    x <=> y
  end
end

···

On Tue, Nov 1, 2011 at 11:21 PM, Wayne Brissette <waynefb@earthlink.net>wrote:

I've got a array that I need sorted in a certain way and I'm struggling to
figure out how to make it happen the way I want.

Basically the array needs to be sorted like this:

[A, B, C, AA, AB, AF ]

that is, a single alpha character before it sorts the next character.
However, what I end up with when I sort is:

[A, AA, AB, AF, B, C]

How would I sort A to Z then AA-ZZ?

Wayne

--
Sincerely,

Isaac Sanders
Section C-4B Vice Chief, Order of the Arrow
Vice Chief of Administration, Tecumseh #65
Eagle Scout

You should probably try the code you're providing as a solution. That
cuts down on the stuff that a) raises exceptions, and b) doesn't solve
the problem even when the exceptions are gone.

···

On Nov 1, 11:34 pm, Isaac Sanders <isaacbfsand...@gmail.com> wrote:

Class: Array (Ruby 1.9.2)

arr.sort do |x,y|
if x.length < y.length
return 1
elsif x.length > y.length
return -1
else
x <=> y
end
end

--
-yossef

Wow, thanks Jan that was it… :slight_smile:

Wayne

···

On Nov 1, 2011, at 10:28 PM, Jan wrote:

array.sort_by {|s| [s.size, s]}

Errr, yah. I almost had it there. sry, I was doing calc hw, and saw the
message, the thought didn't process....

···

On Tue, Nov 1, 2011 at 11:49 PM, Yossef Mendelssohn <ymendel@pobox.com>wrote:

On Nov 1, 11:34 pm, Isaac Sanders <isaacbfsand...@gmail.com> wrote:
> http://www.ruby-doc.org/core-1.9.2/Array.html#method-i-sort
>
> arr.sort do |x,y|
> if x.length < y.length
> return 1
> elsif x.length > y.length
> return -1
> else
> x <=> y
> end
> end

You should probably try the code you're providing as a solution. That
cuts down on the stuff that a) raises exceptions, and b) doesn't solve
the problem even when the exceptions are gone.

--
-yossef

--
Sincerely,

Isaac Sanders
Section C-4B Vice Chief, Order of the Arrow
Vice Chief of Administration, Tecumseh #65
Eagle Scout

array.sort_by {|s| [s.size, s]}

That's an incredibly elegant solution, thanks for sharing! :slight_smile:

···

On Nov 1, 2011, at 10:28 PM, Jan wrote:

On 2 November 2011 10:56, Sam Rose <samwho@lbak.co.uk> wrote:

On Nov 1, 2011, at 10:28 PM, Jan wrote:

array.sort_by {|s| [s.size, s]}

That's an incredibly elegant solution, thanks for sharing! :slight_smile:

On 2 November 2011 08:53, Wayne Brissette <waynefb@earthlink.net> wrote:

On Nov 1, 2011, at 10:28 PM, Jan wrote:

array.sort_by {|s| [s.size, s]}

Wow, thanks Jan that was it… :slight_smile:

Wayne

In case you find the temporary structure created for sorting inelegant
you can do this:

irb(main):006:0* a = %w{A AA AB AF B C}
=> ["A", "AA", "AB", "AF", "B", "C"]
irb(main):007:0> a.sort {|x,y| (x.length <=> y.length).nonzero? or x <=> y}
=> ["A", "B", "C", "AA", "AB", "AF"]

Kind regards

robert

···

On Wed, Nov 2, 2011 at 12:28 PM, Sam Rose <samwho@lbak.co.uk> wrote:

On Nov 1, 2011, at 10:28 PM, Jan wrote:

array.sort_by {|s| [s.size, s]}

That's an incredibly elegant solution, thanks for sharing! :slight_smile:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/