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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:53:23+00:00 2026-05-18T20:53:23+00:00

Sorry for the newb question, everyone. Here it is: I have a hash that

  • 0

Sorry for the newb question, everyone. Here it is:

I have a hash that looks like this:

{ "id" => { :task => [ { :due => Mon Dec 20 00:00:00 UTC 2010, completed: => "2010-12-18T17:29:57Z", :priority => "1", ... } ] , ... } , ... }

To sort this, I use:

tasks = hash.with_indifferent_access

tasks.sort_by { |k,v| [ v['task'][0]['completed'], v['task'][0]['due'], v['task'][0]['priority'] ] }

This works fine as long as :due has a date value. When it doesn’t have a date value, which is permitted, it looks like this:

:due => ""

Then I get a Rails error saying: “comparison of Array with Array failed.”

I tried putting in ternary and other logic to default to a distant date if :due is empty, but it seems this isn’t possible in the sort_by block.

Any ideas how to lick this one? Many 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-05-18T20:53:24+00:00Added an answer on May 18, 2026 at 8:53 pm

    Here is an example assuming that “due” is a string and when empty you want it to sort before other tasks with the same completion value. The idea is to convert both valid dates as well as empty strings into the same comparable data type (in this case the integer number of seconds since the epoch). I have intentionally ignored details of your setup that are irrelevant to your question.

    # Required for Time.parse
    require 'time'
    
    tasks = [
      {
        completed: "2010-12-18T17:29:57Z",
        due:       "Mon Dec 20 00:00:00 UTC 2010",
        priority:  "1"
      },{
        completed: "2010-12-18T17:29:57Z",
        due:       "Mon Dec 20 00:00:00 UTC 2010",
        priority:  "2"
      },{
        completed: "2010-12-18T17:29:57Z",
        due:       "",
        priority:  "1"
      },{
        completed: "2010-12-17T17:29:57Z",
        due:       "Mon Dec 20 00:00:00 UTC 2010",
        priority:  "1"
      },{
        completed: "2010-12-19T17:29:57Z",
        due:       "Mon Dec 20 00:00:00 UTC 2010",
        priority:  "1"
      }
    ]
    
    require 'pp'
    pp tasks.sort_by{ |h| [
      Time.parse(h[:completed]),
      h[:due].empty? ? 0 : Time.parse(h[:due]).to_i,
      h[:priority].to_i
    ]}
    #=> [{:completed=>"2010-12-17T17:29:57Z",
    #=>   :due=>"Mon Dec 20 00:00:00 UTC 2010",
    #=>   :priority=>"1"},
    #=>  {:completed=>"2010-12-18T17:29:57Z", :due=>"", :priority=>"1"},
    #=>  {:completed=>"2010-12-18T17:29:57Z",
    #=>   :due=>"Mon Dec 20 00:00:00 UTC 2010",
    #=>   :priority=>"1"},
    #=>  {:completed=>"2010-12-18T17:29:57Z",
    #=>   :due=>"Mon Dec 20 00:00:00 UTC 2010",
    #=>   :priority=>"2"},
    #=>  {:completed=>"2010-12-19T17:29:57Z",
    #=>   :due=>"Mon Dec 20 00:00:00 UTC 2010",
    #=>   :priority=>"1"}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the log4net newb quesiton, this looks like a common setup, but I
sorry if this is a newB question, please conider the following code: #include <boost/multi_index_container.hpp>
First post here so sorry if I seem like a newb, I am trying
sorry for this newb question, i just started programming with php I'm following Yahoo
Sorry this is a total newb question but there is this really great C-library
First I'm so newb to iPhone dev and I'm sorry if this is easy.
I am a newcomer to python (also very sorry if this is a newb
Sorry if the question sounds confusing. My problem is that I am using the
Sorry if this question is framed incorrectly. I am working on an iPad application
Sorry for the newb question but I am an experienced old-school programmer but I

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.