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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:27:24+00:00 2026-06-10T20:27:24+00:00

I make my hash in the order listed below and I sort it in

  • 0

I make my hash in the order listed below and I sort it in ascending order fine, but when I want to sort it in descending order I don’t get the correct result.

Hash is this

hash_answers = {}
unless answers.blank?
  answers.each_with_index do |ans, index|
    voted_up_users = ans.votes_up_by_all_users(ans)
    voted_down_users = ans.votes_down_by_all_users(ans)

    hash_answers[ans.id] = {
        :id => ans.id,
        :count => voted_up_users.count - voted_down_users.count,
                                       :created_at => ans.created_at
    }
  end
end

This code above works fine when I sort it in ascending order by created_at and count base

   answers = hash_answers.sort_by { |k, v| [ v[:count] , v[:created_at] ] } 

but how can I sort this hash in descending order by created_at and on base of count.

Help if anyone can
Thanks

  • 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-10T20:27:25+00:00Added an answer on June 10, 2026 at 8:27 pm

    You can use block in sort method, in block you should provide the sort rule, in your case it might be like this:

    temp = hash_answers.sort do |h1,h2| 
      v1 = h1.last; v2 = h2.last
      [v2[:count],v2[:created_at]] <=> [v1[:count],v1[:created_at]]
    end
    
    answer = Hash[temp]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a md5 hash, and just want to make sure the result
$('#head_menu a').click(function(){ currentPage = document.location.hash.split('#')[1]; }); <div id=head_menu> <a href=#order><div>make order</div></a> <a href=#portfolio><div>portfolie</div></a> <a
I have hash (@post) of hashes where I want to keep the order of
I have to write my own hash function. If I wanted to just make
I make a bot program and i want to send some keyboard keys between
I have attached certain actions to the existence of a URL hash in order
In order to make the syntax for one of my functions nicer, I need
I have a boost::unordered_map, but it appears to be in order, giving me an
make /bin/sh /Users/myusername/Downloads/php-5.3.5/ext/mssql/libtool --mode=compile cc -I. -I/Users/myusername/Downloads/php-5.3.5/ext/mssql -DPHP_ATOM_INC -I/Users/myusername/Downloads/php-5.3.5/ext/mssql/include -I/Users/myusername/Downloads/php-5.3.5/ext/mssql/main -I/Users/myusername/Downloads/php-5.3.5/ext/mssql -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM
Make it on Linux. The reason to use more than one version of Vim,

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.