-3

In the following, the results are the same:

[3, 5].sort{|a, b| b <=> a}
[5, 3].sort{|a, b| b <=> a}

I would like to know what happened internally and how it depends on input array.

pramod
  • 2,258
  • 1
  • 17
  • 22
  • The two lines of code you posted sort two different permutations of the elements of the same array. They **must** produce the same outcome (sort the values according to your sorting rules). – axiac May 10 '17 at 10:41
  • @axiac could you please explain how the sort block is going to evaluate. I think it depends on the input array – pramod May 10 '17 at 10:48
  • If they give different results, what is the purpose of sorting? – sawa May 10 '17 at 11:00
  • @sawa I'm looking for explanation. I just accepted Stefan answer. Please have a look at once – pramod May 10 '17 at 11:02
  • 1
    @pramod of course it depends of the input array but the purpose of [`Array#sort()`](http://ruby-doc.org/core-2.4.0/Array.html#method-i-sort) is to return an array that contains the same values as the input array in a specified order. As long as you use the same sorting rules (`b <=> a` in this case), `#sort()` must always return the same result, no matter the order of values in the input array. That's the only reason `Array#sort()` exists. – axiac May 10 '17 at 11:08
  • 1
    @axiac: Actually, `Enumerable#sort` and `Enumerable#sort_by` are not stable, so it is in fact *not* guaranteed that the result is always the same. It is in fact not even guaranteed to be the same for *the same* input. – Jörg W Mittag May 10 '17 at 12:15

3 Answers3

3

The first line:

[3, 5].sort { |a, b| b <=> a }

Invokes the block with a = 3 and b = 5. It returns the result of 5 <=> 3 which is 1. An integer greater than 0 tells sort that a follows b. The result is therefore [5, 3].


The second line:

[5, 3].sort { |a, b| b <=> a }

Invokes the block with a = 5 and b = 3. It returns the result of 3 <=> 5 which is -1. An integer less than 0 tells sort that b follows a. The result is therefore (again) [5, 3].

Stefan
  • 109,145
  • 14
  • 143
  • 218
1

Because you are sorting an array, and changing the array's elements order does not change the sorting result.

This is the whole point of sorting after all - to get sorted result despite the initial arrays ordering.

To change the result, you will want to change the sorting rule, not the array.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • It make sense to me, could you please explain how the block is going to evaluate. I think it depends on the input array. – pramod May 10 '17 at 10:44
  • @pramod [please see this thread](http://stackoverflow.com/questions/2637419/how-does-arraysort-work-when-a-block-is-passed) on the `sort` with block explanation. It does not depend on the "input array" :) – Andrey Deineko May 10 '17 at 10:47
1

The output is the same regardless of the input order because you sorting the array.

If you want to sort with the opposite order write

[3,5].sort{|a,b| a <=> b}
user000001
  • 32,226
  • 12
  • 81
  • 108