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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:37:07+00:00 2026-06-04T16:37:07+00:00

ive been trying to sort an array in desc order but have been having

  • 0

ive been trying to sort an array in desc order but have been having trouble doing so.

i have…

microposts = Micropost.from_users_followed_by(self)
pub_messages = PubMessage.find_pub_messages_for(self.id)
(microposts + pub_messages).sort! { |a,b| a[:time_ago] <=> b[:time_ago] }

however the last line returns everything in ascending order. doing

.reverse

fixes it, but i heard it’s not very efficient to do so. i tried doing

(microposts + pub_messages).sort! { |a,b| -a[:time_ago] <=> -b[:time_ago] }

but it gives an error that it can’t find -@

i tried

(microposts + pub_messages).sort! { |a,b| b[:time_ago] <=> a[:time_ago] }

but it doesn’t change it either (i swapped b and a around). what am i doing wrong? in the meantime, when i do the methods

find_pub_messages_for and from_users_followed_by

i do something like

default_scope order: 'pub_messages.created_at DESC'
default_scope order: 'microposts.created_at DESC'

in my models when i retrieve them. however is that a waste? because in the end, im sorting them again based on both microposts and pub_messages as seen in the line

(microposts + pub_messages).sort! { |a,b| a[:time_ago] <=> b[:time_ago] }

thanks a bunch!

  • 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-04T16:37:09+00:00Added an answer on June 4, 2026 at 4:37 pm
    (microposts + pub_messages).sort! { |a,b| b[:time_ago] <=> a[:time_ago] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been trying to sort out output using AWK, and have been pretty successful
i have been over so many posts online trying to sort this.... i am
I have been trying to load my pre-filled database into my device, ive tried
Just need help as I have been trying sort this out for ages now.
I've been trying to sort a table using ajax for sometime now but the
I have been trying to implement various types of sort in a program am
I've been trying to sort this out for a while now, I have checked
I've been trying to get a sort of rudimentary filter working. Basically, you click
I've been trying to set up some sort of geometry batching for a week
I've been Googling for a while now trying to find some sort of solution

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.