0

I have 3 courses A(july 1), B(july 2), C(july 3).A and B is rated 4 and C is rated 5.

I want to order the course like this

C should come first because it was created latest and it has higher rating than others.

A should come second because it was created first than B

I cant use order because it wont give me what i need. any way to fix this?

Here is how i am fetching the data

@courses.order('updated_at DESC, average_rating DESC')

code

[
#<Course:0x00000009f3c128
  id: 6,
  tutor_id: 2,
  course_name: "name",
  course_subtitle: "sub",
  course_description: "<p>test</p> test\r\n",
  video_link: "https://www.youtube.com/watch?v=UVrQcieqD0U",
  course_language: "German",
  course_image: "finalse.png",
  created_at: Tue, 11 Jul 2017 05:03:03 UTC +00:00,
  updated_at: Tue, 11 Jul 2017 08:47:03 UTC +00:00,
  status: "accepted",
  average_rating: 2.5,
  rated_time: nil>,
 #<Course:0x00000008139608
  id: 7,
  tutor_id: 2,
  course_name: "another",
  course_subtitle: "another subtuitle",
  course_description: "<p>course descrition</p>\r\n",
  video_link: "https://www.youtube.com/watch?v=uaTeZA-Gj7s",
  course_language: "Chinese",
  course_image: nil,
  created_at: Tue, 11 Jul 2017 10:40:45 UTC +00:00,
  updated_at: Tue, 11 Jul 2017 10:41:06 UTC +00:00,
  status: "accepted",
  average_rating: 2.5,
  rated_time: nil>,
 #<Course:0x0000000813bea8
  id: 8,
  tutor_id: 2,
  course_name: "asfas",
  course_subtitle: "were",
  course_description: "<p>asdfsadf</p>\r\n",
  video_link: "https://www.youtube.com/watch?v=xGytDsqkQY8",
  course_language: "English",
  course_image: nil,
  created_at: Wed, 12 Jul 2017 03:53:26 UTC +00:00,
  updated_at: Wed, 12 Jul 2017 04:32:33 UTC +00:00,
  status: "accepted",
  average_rating: 1.0,
  rated_time: nil>,
4
  • Please, show your code. Commented Jul 12, 2017 at 5:54
  • @timiTao, question updated Commented Jul 12, 2017 at 5:57
  • I guess you could sort it with something like: [pseudocode] if rate1 == rate2 then return time2 - time1 else return rate1 - rate2, assuming there is a sort method that takes a callback taking two parameters and returning the difference Commented Jul 12, 2017 at 7:43
  • i will have to loop the whole data for this, do not I ? Commented Jul 12, 2017 at 7:48

2 Answers 2

1

Try:

Course.all.order("average_rating DESC, created_at ASC")
Sign up to request clarification or add additional context in comments.

3 Comments

the only problem with this is that if A, B has a rating of 4 each but A was created first and B was created second, then B shows up first. If A had shown up first, I wud accept this answer
you can change it to created_at ASC
@SushantBajracharya : you don't need to define ASC as well, as default it take ASC order
0

try Course.order({ created_at: :desc, rating: :desc })

This will sort first on created_at and if two records have same created_at the will sort on the basis of rating

4 Comments

well, this should work. Could you please share the query fired on rails console. So that I can have a better idea of what's happening
It is sorting on the basis of time correctly. But your problem is that the field is of type datetime so, no two records have same created_at value.
it has to sort according to rating as well.
see there may be following cases According the query above it will sort by rating only when it got two records with same created_at value. Which will obviously never be the case. Please tell me what should be result if Course A created_at 4July 4:03 rating 2 Course B created_at 4July 3:03 rating 3 Course C created_at 5July 4:10 raitng 1 Now should Course C should be at top because it has highest created_at or at the lowest because it has lowest rating

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.