COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### how does sort work?

• Email
• Follow

```I'm confused as to how the sort method works.

If I have:
a = [ "d", "a", "e", "c", "b" ]

I know that
a.sort {|x,y| x <=> y } (or just .sort)
= ["a", "b", "c", "d", "e"]

and
a.sort {|x,y| y <=> x }
= ["e", "d", "c", "b", "a"]

I understand how the <=> operator works, but what exactly is going on in
the block? What are x and y assigned to?
--
Posted via http://www.ruby-forum.com/.

```
 0
Reply chris.sepic (2) 2/2/2008 11:39:50 PM

See related articles to this posting

```"Chris Sepic" <chris.sepic@gmail.com> wrote in message
news:3be71570cbfeb7f18c389ab412785977@ruby-forum.com...
> I'm confused as to how the sort method works.
>
> If I have:
> a = [ "d", "a", "e", "c", "b" ]
>
> I know that
> a.sort {|x,y| x <=> y } (or just .sort)
> = ["a", "b", "c", "d", "e"]
>
> and
> a.sort {|x,y| y <=> x }
> = ["e", "d", "c", "b", "a"]
>
> I understand how the <=> operator works, but what exactly is going on in
> the block? What are x and y assigned to?

I'm used to sorting not be a language concept.  I think I'm gonna like Ruby.

I think this operator is overloaded, but in this context means "comparison."
In the index of Mats' .chm file in the docs,  I find several different
entries.

--
Gerry Ford

"The apple was really a peach."
-- Allison Dunn on the garden of eden

```
 0
Reply invalid163 (957) 2/3/2008 12:41:08 AM

```Chris Sepic wrote:
> I'm confused as to how the sort method works.
>
> If I have:
> a = [ "d", "a", "e", "c", "b" ]
>
> I know that
> a.sort {|x,y| x <=> y } (or just .sort)
> = ["a", "b", "c", "d", "e"]
>
> and
> a.sort {|x,y| y <=> x }
> = ["e", "d", "c", "b", "a"]
>
> I understand how the <=> operator works, but what exactly is going on in
> the block? What are x and y assigned to?

Sorting arrays is a complex topic that has been given a lot of attention
in computer programming.  The goal is to sort the array with the fewest
comparisons, i.e. to sort it as fast as possible.  You can google the
topic and read about different sorting methods--you might start by
searching 'bubble sort'.

The sort method  uses an "algorithm" (or methodology) to sort the array.
In order to sort an array you have to compare the elements in the array.
The fewer times you have to compare the elements to arrive at a sorted
array the better.  When ruby needs to compare two elements in the
process of sorting an array, ruby assigns the two elements to the
variables x and y.  Your block then decides which of the two elements
should come first in the array.  Ruby takes that result and proceeds
with the process of sorting the array.

It should be obvious to you that your block has to be called more than
once in order for ruby to sort your array--you can't sort a 5 element
array by doing one comparison of two of the elements.  But what is the
minimum number of comparisons ruby needs to make in order to sort your
array?

--
Posted via http://www.ruby-forum.com/.

```
 0
Reply bbxx789_05ss (1895) 2/3/2008 1:45:58 AM

```Chris Sepic wrote:
> I'm confused as to how the sort method works.
>
> If I have:
> a = [ "d", "a", "e", "c", "b" ]
>
> I know that
> a.sort {|x,y| x <=> y } (or just .sort)
> = ["a", "b", "c", "d", "e"]
>
> and
> a.sort {|x,y| y <=> x }
> = ["e", "d", "c", "b", "a"]
>
> I understand how the <=> operator works, but what exactly is going on in
> the block? What are x and y assigned to?

to understand this, you can think of "sort" taking a function as a
parameter.  and sort() will pass 2 numbers to this function, and
rearragne the elements according to the return value of -1, 0, or 1.

that's it.  to understand it fully, you can read about "iterators" in
Ruby.  where you can take a block as a function, and "yield" arguments
to this function, and get back a return value.

--
Posted via http://www.ruby-forum.com/.

```
 0
Reply Summercoolness (256) 2/3/2008 2:02:25 AM

```> to understand this, you can think of "sort" taking a function as a
> parameter.  and sort() will pass 2 numbers to this function, and
> rearragne the elements according to the return value of -1, 0, or 1.
>
> that's it.  to understand it fully, you can read about "iterators" in
> Ruby.  where you can take a block as a function, and "yield" arguments
> to this function, and get back a return value.

I understand sorting and sorting algorithms - I was tripped up by the
function as a parameter thing. Thanks!

--
Posted via http://www.ruby-forum.com/.

```
 0
Reply chris.sepic (2) 2/3/2008 3:09:25 AM

```On Feb 2, 2008 8:02 PM, SpringFlowers AutumnMoon
<summercoolness@gmail.com> wrote:
> Chris Sepic wrote:
> > I'm confused as to how the sort method works.
> >
> > If I have:
> > a = [ "d", "a", "e", "c", "b" ]
> >
> > I know that
> > a.sort {|x,y| x <=> y } (or just .sort)
> > = ["a", "b", "c", "d", "e"]
> >
> > and
> > a.sort {|x,y| y <=> x }
> > = ["e", "d", "c", "b", "a"]
> >
> > I understand how the <=> operator works, but what exactly is going on in
> > the block? What are x and y assigned to?
>
>
> to understand this, you can think of "sort" taking a function as a
> parameter.  and sort() will pass 2 numbers to this function, and
> rearragne the elements according to the return value of -1, 0, or 1.

That's right.  Basically, #sort gives you, on each iteration, two
elements out of your list based on it's own sort algorithm (I think it
uses a quicksort version?).  Your code can do whatever it wants to
with these elements, as long as your block returns a -1, 0, or 1 each
time.  For example, your sort may be based on a suffix instead of a
beginning character or number (i.e. hexagon vs pentagram when the
order I want is n-grams to come before n-gons).  Thus, you can define
the characteristics that must be sorted by, and those characteristics
can be sophisticated if you need them to be.

Todd

```
 0

```"Todd Benson" <caduceass@gmail.com> wrote in message
news:eaeff8c10802021913w17eccc0ch31d9a9b18af78dfd@mail.gmail.com...
> On Feb 2, 2008 8:02 PM, SpringFlowers AutumnMoon
> <summercoolness@gmail.com> wrote:
>> Chris Sepic wrote:
>> > I'm confused as to how the sort method works.
>> >
>> > If I have:
>> > a = [ "d", "a", "e", "c", "b" ]
>> >
>> > I know that
>> > a.sort {|x,y| x <=> y } (or just .sort)
>> > = ["a", "b", "c", "d", "e"]
>> >
>> > and
>> > a.sort {|x,y| y <=> x }
>> > = ["e", "d", "c", "b", "a"]
>> >
>> > I understand how the <=> operator works, but what exactly is going on
>> > in
>> > the block? What are x and y assigned to?
>>
>>
>> to understand this, you can think of "sort" taking a function as a
>> parameter.  and sort() will pass 2 numbers to this function, and
>> rearragne the elements according to the return value of -1, 0, or 1.
>
> That's right.  Basically, #sort gives you, on each iteration, two
> elements out of your list based on it's own sort algorithm (I think it
> uses a quicksort version?).  Your code can do whatever it wants to
> with these elements, as long as your block returns a -1, 0, or 1 each
> time.  For example, your sort may be based on a suffix instead of a
> beginning character or number (i.e. hexagon vs pentagram when the
> order I want is n-grams to come before n-gons).  Thus, you can define
> the characteristics that must be sorted by, and those characteristics
> can be sophisticated if you need them to be.
>
> Todd
>

Why do you put a hash in front of sort?

--
Gerry Ford

"The apple was really a peach."
-- Allison Dunn on the garden of eden

```
 0
Reply invalid163 (957) 2/3/2008 3:37:57 AM

```"7stud --" <bbxx789_05ss@yahoo.com> wrote in message
news:6bb4c82932a54398bda16f052b3c7d00@ruby-forum.com...

> It should be obvious to you that your block has to be called more than
> once in order for ruby to sort your array--you can't sort a 5 element
> array by doing one comparison of two of the elements.  But what is the
> minimum number of comparisons ruby needs to make in order to sort your
> array?
I think 5-element sorts are different from 4- and 6-, and for different
reasons.

A4 has cardinality 4!  Nothing goes wrong at this number.  A5 has
cardinality 60, the order of an important finite, simple group.  Things
break down between four and five, for example, the theory of equations.

On the other side of five is six.  Somewhere between five and six is where
mankind falls, if he does a binary sort with 2^n elements.  Five means your
chugging along on five different tracks.  Old Bach could do six on his
organ.

At UPS, we had 16 belts.  There were advantages to duality.  It was a  2^4
gig.

Error on g-string, is, of course a six.

--
Gerry Ford

"The apple was really a peach."
-- Allison Dunn on the garden of eden

```
 0
Reply invalid163 (957) 2/3/2008 4:10:59 AM

```On 03.02.2008 04:09, Chris Sepic wrote:
>> to understand this, you can think of "sort" taking a function as a
>> parameter.  and sort() will pass 2 numbers to this function, and
>> rearragne the elements according to the return value of -1, 0, or 1.
>>
>> that's it.  to understand it fully, you can read about "iterators" in
>> Ruby.  where you can take a block as a function, and "yield" arguments
>> to this function, and get back a return value.
>
> I understand sorting and sorting algorithms - I was tripped up by the
> function as a parameter thing. Thanks!

Yeah, a block is an anonymous function.

You can see how it works if you insert a print statement:

irb(main):001:0> a = [ "d", "a", "e", "c", "b" ]
=> ["d", "a", "e", "c", "b"]
irb(main):002:0> a.sort {|x,y| p [x,y]; x<=>y}
["d", "e"]
["e", "b"]
["d", "b"]
["a", "d"]
["c", "d"]
["b", "a"]
["a", "c"]
["b", "c"]
=> ["a", "b", "c", "d", "e"]
irb(main):003:0>

A nice quiz might be to find out the sorting algorithm based on this
output. :-)

Kind regards

robert
```
 0
Reply shortcutter (5831) 2/3/2008 5:20:05 PM

8 Replies
52 Views

Similar Articles

12/9/2013 6:23:14 PM
page loaded in 328015 ms. (1)