1

I want to sort by rating DESC. It works with MySQL but on PostgreSQL. I get a different result.

You can see the problem here: http://www.vinderhimlen.dk/konkurrencer

My controller:

  def sort_column
    Konkurrancer.column_names.include?(params[:sort]) ? params[:sort] : "rating"
  end

  def sort_direction
    %w[desc asc].include?(params[:direction]) ? params[:direction] : "desc"
  end
1
  • It seems to be working fine to me, except for the ones with 0 ratings. Is there any chance you could give us the SQL you're using, and some idea of the data it's running against? Commented Jun 1, 2011 at 10:49

1 Answer 1

5

Not sure what your issue is exactly or how it "doesn't work", from lack of details in your question. But at least two factors can affect sorting in such a way that you'd get different results in MySQL and PostgreSQL.

The first is collation. In particular if you're playing with 9.1 beta. Last I installed MySQL (which was a while ago, so they might have fixed this since), it was collating things as latin-1/swedish by default, vs utf-8/english for PostgreSQL.

The other is nulls. MySQL always places these last if memory serves. By contrast, PostgreSQL consistently places them at the end of btree indexes, and thus places them last when ordering asc and first when ordering desc. You can change this behavior by ordering using nulls first/nulls last.

In your particular case, my guess is that you want to order by rating desc nulls last, as opposed to the default behavior which will place nulls first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.