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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:57:38+00:00 2026-06-01T05:57:38+00:00

I have an array of appointment objects, and I would like to sort by

  • 0

I have an array of appointment objects, and I would like to sort by starts_at attribute.

my_array.uniq.sort_by! {|obj| obj.starts_at}

However, I got the following error:
comparison of ActiveSupport::TimeWithZone with nil failed

I am sure I don’t have any nil value for starts_at. What’s the problem here?
Also, how can I sort by descending?

Update 1: I realized that uniq! gives me nil when I call .all method to my Appointment class. Without uniq, it works fine.

Appointment.all.uniq!

Update 2: I can’t apply minus to obj.starts_at. It gives me error:method – can’t be found with UTC time format. Probably b/c time does not allowed to be zero. Any thoughts on overcome this?

Update 3: my_array.uniq.sort { |x, y| (x.starts_at || nil) <=> (y.starts_at || nil)} works in console, but when I ran my app, it says comparison of Event with Event failed

Last update: The following works:

@events.sort_by!{|obj| (obj.starts_at.nil? ? 3.years.ago : obj.starts_at.utc)}

I need to convert date format to .utc, and handle missing starts_at (sorry I don’t know why I have missing starts_at attribute on an object)

  • 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-01T05:57:39+00:00Added an answer on June 1, 2026 at 5:57 am

    I have association of blog_post has many blog_comments and i sort array of blog_post according

    to blog_comments count and it is running fine……

      @sort_blog = @blog_posts.sort_by { |i| i.blog_comments.count }
    

    I think you are doing fine but problem is some where you have nil object you can try like this

      my_array.uniq.sort_by! {|obj| obj.starts_at  unless obj.blank?}
    

    Try it………

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

Sidebar

Related Questions

I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
An array is defined of assumed elements like I have array like String[] strArray
Have an array of chars like char members[255]. How can I empty it completely
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
I have array like below, Array ( [14289] => Array ( [0] => Karthikeyan
i have array like below which is sorted by array_count_values function, Array ( [Session
I have array like below: Array ( [_edit_lock] => Array ( [0] => 1309611144:1
I have array like this: $test = array(20, 30, 40); And I want to
I have array like this: $test = array(20, 30, 40); And I want to
I have array that looks like this, Array ( [email_address] => Array ( [0]

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.