gui12344455
gui12344455

Reputation: 23

Ruby array sorting order

I have this array returned by a activerecord query:

SELECT id, value FROM table ORDER BY value DESC
[1054, 86], [1062, 86], [1059, 84]

And I've created a simple array to compare with the first array:

array.sort_by! { |object| object.access_count }.reverse }

But when I compare the two arrays the result is different:

activerecord => [1054, 86], [1062, 86], [1059, 84]
array.sort_by! => [[1062, 86], [1054, 86], [1059, 84]

So in some cases the compare doesn't work because activerecord sorts one way and sort_by! sorts another.

How can I solve that ? Thanks.

Upvotes: 0

Views: 36

Answers (1)

Ronan Louarn
Ronan Louarn

Reputation: 472

Can you try order Activerecord with second arg.

.order(value: :desc, id: :desc)

Upvotes: 1

Related Questions