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 9191859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:47:18+00:00 2026-06-17T20:47:18+00:00

Consider a class Team, with class variables ‘wins’ and ‘losses’. I wish to sort

  • 0

Consider a class Team, with class variables ‘wins’ and ‘losses’. I wish to sort an array of Teams by the win:loss ratio, such that:

5:0 > 3:0 > 1:0 > 3:1 > 5:5 > 3:3 > 1:3 > 0:1 > 0:3 > 0:5

I already have a (partial) hacked together solution that I’m not happy with, and I’m wondering if there is a cleaner/more elegant/simpler way to solve this problem.

def ratio
    if @losses == 0 then 
        return 1000000+@wins
    end
    if @wins == 0 then
        return 0-@losses
    end
    return @wins/@losses
end

(This does not fix 5:5 > 3:3)

Which would be in the Team class and could be used like this:

teams.sort! { |a, b| b.ratio <=> a.ratio }

What is the simplest way to solve this? (Solutions does not have to be Ruby, I’m happy with anything that is OO)

  • 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-06-17T20:47:19+00:00Added an answer on June 17, 2026 at 8:47 pm

    I don’t speak Ruby, but a Python approach which would give your desired results would use a tuple for a key (in the decorate-sort-undecorate “Schwartzian transform” idiom.)

    For example, you could rank by win fraction, then number of wins, then (negative) number of losses, which would give your desired ordering:

    >>> wl = [[3, 0], [3, 1], [0, 5], [3, 3], [0, 3], [5, 5], [1, 3], [5, 0]]
    >>> 
    >>> def rank(wl):
    ...     win, loss = wl
    ...     return (1.0*win/(win+loss) if win+loss > 0 else 0), win, -loss
    ... 
    >>> sorted(wl, key=rank)
    [[0, 5], [0, 3], [1, 3], [3, 3], [5, 5], [3, 1], [3, 0], [5, 0]]
    >>> sorted(wl, key=rank)[::-1]
    [[5, 0], [3, 0], [3, 1], [5, 5], [3, 3], [1, 3], [0, 3], [0, 5]]
    

    I don’t know what the Ruby equivalent is, unfortunately, but I gather that there’s a sort_by method floating around somewhere.

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

Sidebar

Related Questions

Consider a class X with N member variables, each of some copiable and movable
consider this class,with no instance variables and only methods which are non-synchronous can we
consider this class: class baseController { /* Action handler array*/ std::unordered_map<unsigned int, baseController*> actionControllers;
Consider following class of custom array template <typename element, unsigned int size> class array
Consider this class hierarchy: Book extends Goods Book implements Taxable As we know, there
Consider this: class User < ActiveRecord::Base # name, email, password end class Article <
Consider: public class HomeController : Controller { private IDependency dependency; public HomeController(IDependency dependency) {
Consider this class: class test { public function __set($n, $v) { echo __set() called\n;
It may not be so obvious how respond_to? works in ruby. Consider that: class
Consider the following: class MyClass { public: int operator ()(int a, int b); };

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.