Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4594320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:48:40+00:00 2026-05-21T22:48:40+00:00

I need to sort league table in Rails 3, so I have LeagueTable model,

  • 0

I need to sort league table in Rails 3, so I have LeagueTable model, Team model and Match model.
Basic sorting is done by points summary, so this is easy part. But when two teams got the same points number, I want to sort them by points won in matches between this two teams.
I got no idea how to do this.

EDIT:

# league_table.rb model
class LeagueTable < ActiveRecord::Base
    belongs_to :team
end


# match.rb model
class Match < ActiveRecord::Base
    belongs_to :team_home, :class_name => "Team"
    belongs_to :team_away, :class_name => "Team"
end


# team.rb model
class Team < ActiveRecord::Base
    has_many :matches
end


# schema.rb
create_table "league_tables", :force => true do |t|
    t.integer  "team_id"
    t.integer  "points"
    t.integer  "wins"
    t.integer  "draws"
    t.integer  "looses"
    t.integer  "goals_won"
    t.integer  "goals_lost"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "matches"
    t.integer  "priority"
  end

  create_table "matches", :force => true do |t|
    t.integer  "team_home_id"
    t.integer  "team_away_id"
    t.integer  "score_home"
    t.integer  "score_away"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "teams", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:48:41+00:00Added an answer on May 21, 2026 at 10:48 pm

    Very interesting question. Here is (more less) how I would handle it, using recursion and the magic of group_by.

    1. You would need a class method that, given an array of teams, calculates the number of points (goals for, goals against…) each team has got in the matches between those teams only. Assume you return a hash of hashes: {team_foo => {:points => 10, :goals_for => 33, :goals_against => 6}, team_bar => {:points => 18, :goals_for => 50, :goals_against => 11}...}.
    2. The final order of the teams will be put into an array final_order, which is empty now.
    3. Take all teams to an array and apply the method mentioned above. You should now have teams and the number of points (goals for, goals against…) all the teams have gained in all the games currently played. Store it somewhere, say under the name all_teams_scores.
    4. Take all teams and group them using group_by: teams.group_by {|t| all_teams_scores[t][:points]}. What you get is an OrderedHash, looking like: {10 => [team_foo], 18 => [team_bar, team_xyz]}. The key is what you group by, the value is always an array. Store this hash, e.g. as league_table.
    5. Sort the keys of league_table (remember to reverse when needed) and check the values of league_table according to them. If the array has two or more elements, apply points 3.-5. to the teams in that array. If the array has one element, append this element to final_order.
    6. Done.

    Some remarks:

    • Remember that in the worst case you are left with two or more teams that have gained the same number of points (goals…) in the direct games. You need to handle that, or sort by random.
    • The above steps group by points only. If you need to first sort by points in all games, then points in direct games, it will work. If you want to sort by points in all games, points in direct games, goals in all games – you need to refine the above algorithm. It should not be hard, though 😉
    • You can group_by an array (which helps when you want to sort over a number of things before or after you sort by direct games).
    • Unless your site is going to be heavily used, generate the table whenever requested. No need to store it in the dabase – unless the season is over and no changes to the game results are to be made.
    • If you decide to store the table in the database, ensure that the fields related to the position of a team in the table are not editable by the user. The best way probably would be to implement the above algorithm as a stored procedure (PostgreSQL supports pl/Ruby, although I never used it), create view based on that and access league table from Rails through it (views are, generally, read only). Of course, removing the access to these fields from the administrative interface is also ok, for 99.99% situations 😉
    • This may not (and probably is not) the optimal solution in terms of speed or memory efficiency (although I have not checked it), but I suppose the code produced according to this method should be easy to read, understand and maintain later on.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to sort then print the result increasing and decreasing. I have some
I have an array which i need to sort its elements by occurrence then
I need to sort some products base on user ratings. Suppose we have 3
I need to sort some products base on user ratings. Suppose we have 3
I need to sort on a date-field type, which name is mod_date. It works
I need to sort close to a 1,00,000 floating point entries in Delphi. I
I need to sort string, and I came up with the following function. def
I need to sort an array that can look like this: $array[4][0] = array('id'
I need to sort an array of months (alphabetical months) according to their months
I need to sort a collection of objects by a utf-8 string property (built

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.