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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:48:12+00:00 2026-05-14T21:48:12+00:00

I have an array of users that’s sorted in descending order based on total_points.

  • 0

I have an array of users that’s sorted in descending order based on total_points.

I need to find the rank of each user in that array. The issue is that more than one user can have the same total points and, thus, the same rank. For example, three users could be in 3rd place with 200 Points. Here’s my current code:

class Leader < ActiveRecord::Base  
  def self.points_leaders
    all_leaders = all_points_leaders # returns array of users sorted by total_points in desc order
    all_leaders_with_rank = []

    all_leaders.each do |user|
      rank = all_leaders.index(user)+1
      all_leaders_with_rank << Ldr.new(rank, user) # Ldr is a Struct
    end

    return all_leaders_with_rank
  end
end

How must I modify the code so that the correct rank is returned, and not just the value of the index position?

  • 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-14T21:48:13+00:00Added an answer on May 14, 2026 at 9:48 pm

    The borderline-brute-force approach would be a simple change to your existing code.

    rank = 1
    all_leaders.each_with_index do |user, idx|
      # If this user has a different point total than the previous user in the list,
      # bump the rank.
      if idx > 0 && all_leaders[idx - 1].total_points != user.total_points
        # The point of using the idx as an offset here is so that you end up with 
        # T1
        # T1
        # T3
        # in the case of a tie for first.
        rank = idx + 1
      end
      all_leaders_with_rank << Ldr.new(rank, user) # Ldr is a Struct
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this block of code: users = Array.new users << User.find(:all, :conditions =>
I am using PHP and I have an array of user images that I
I have a array with users sorted by score. I only want to output
I have the code: $getCookieData = $this->Cookie->read('data'); $getUser = $this->User->find('first', array('conditions' => array('User.username' =>
Need your help with my PHP/MYSQL array. I have a php script that selects
I have data that is in an array, these are the ID's for users
I have an array (_users) that contains JSON objects. { User: { userid:19571, status:7,
I Have an Array of users that i want to pass into a view
I have a select_tag populated from a @users array that I'm using to perform
I have code like this, that initialize config $this->config = array( 'users' => array(

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.