60991

Ruby sort_by for arrays returned by MySQL, date formatted as string

Question:

I have a database that has a task table. In that table, there is a date column. Those dates are formatted as strings, they aren't Date.

I'm trying to sort these tasks by date. I already have an array of the tasks named tasks. I'm trying to replace it with the sorted array called tasksByDate using the below code.

tasksByDate = tasks.sort_by do |task| task[:date].to_date end

The error I'm getting is:

TypeError: no implicit conversion of Symbol into Integer

I also tried without to_date just to see if it would sort it without it being a date, and just being a string.

The date field is formatted as a string like so 2016-08-29. I used the to_date method on it somewhere else in the code, and it works great, so I didn't really think that was the problem.

<hr />

<strong>Edit 1</strong>

I have checked that tasks actually contains a date, and it is formatted like explained.

The output of p task.class is Array

<strong>Edit 2</strong>

The output of p task is

[#<User id: 10, login: "my.name", hashed_password: "", date: "2016-08-29">]

Answer1:

The elements appear to be nested deeper than you expected them to be. Change the your code to:

# use '{ }' instead of 'do end' for a single-line blocks tasksByDate = tasks.sort_by { |task| task.first[:date].to_date }

Explanation:

What you see as an output of p task:

[#<User id: 10, login: "my.name", hashed_password: "", date: "2016-08-29">]

It means that this is an Array of elements. Notice the enclosing braces [ ]. So what you have to do in this case is task.first, which will return:

#<User id: 10, login: "my.name", hashed_password: "", date: "2016-08-29">

From there you should be able to access the element's values by a key, like you intended:

task.first[:date]

Recommend

  • Sorting points from distance to a given point x,y here in my case (x=0,y=o)
  • SSRS Rendering format PPT / PPTX
  • How can I get text under mouse pointer WebBrowser control?
  • Outlook Addin to add custom field to mail form or accessing existing field with c#
  • Remove Duplicate Numbers and Operators from String
  • C#: How to use custom autocomplete source for textbox
  • Generate n unique random integers in ruby / rails
  • Sort hash by nested value
  • The remote server returned an error: (440) Login Timeout
  • Loop through each value to the seq num
  • CameraCaptureTask missing in Window Phone 8.1 runtime???
  • dyanmic url not rewriting ht access
  • Connect unmanaged c++ to WCF using TCP Binding
  • How to sort a World Cup group table in Ruby
  • In C,why is definition of a global variable in a separate statement raising warning,but is OK for a
  • How can I pass complex expression to parametrized active pattern?
  • Best style for iterating over a small number of items in Python?
  • Installing ocropus-0.4.4
  • jQuery file download plugin
  • Can one add a complex type item to ListModel?
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • JSON Error when parsing “… has no method 'replace'”
  • Type safe keys with Entity Framework model
  • RavenDB indexing errors
  • how to solve invalid conversion specifier warning in iphone app
  • abstracting over a collection
  • Function calls are not supported. Consider replacing the function or lambda with a reference to an e
  • SAXReader not re-ecape characters
  • How to revert to previous XCode version?
  • How can I extract results of aggregate queries in slick?
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Programmatically clearing map cache
  • java string with new operator and a literal