# How to Sort by Date if my date fields are not formatted as Dates

**URL:** <https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465>\
**Category:** Help\
**Created:** [May 24, 2020, 3:05pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465 "2020-05-24T15:05:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhogue](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhogue/32/1815_2.png) [@jhogue](https://talk.jekyllrb.com/u/jhogue)\
**Post date:** [May 24, 2020, 3:05pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/1 "2020-05-24T15:05:46Z")

</div>

I have a site that I am porting over from badly coded PHP to Jekyll. My site has a YML data file like `_data/comments.yml` that I use to store comments. The legacy site didn’t store the date for comments for some time and then started to store the date later, so my entries are mixed. And as I understand it, the dates are not well formatted so they are being treated like strings. Here is an example:

```auto
- property: a-url-slug
  person: 'This guy'
  date: 'Mar 23, 2020'
  comment: 'A comment'
- property: a-different-url-slug
  person: 'Another guy'
  date: ''
  comment: 'A comment'

```

My query to get everything out of this data file right now is this:

```auto
{% assign comments = site.data.comments | orderby: 'date' %}

```

But the order that items come back in is mixed, like this:

```auto
Bob Zampini – June 29, 2010
Jim Kelly – May 22, 2008
Mike Blake – 
Bob Renell – 
Robert Yarbrough – May 4, 2018
Pete F. – March 2, 2016
Ernie Hanson – November 6, 2015

```

This seems to be because the date in my YML is not stored as a proper machine readable date. It’s readable and parsable when using `date` filters, which work on well-formatted strings, but the date is not an object that a query will reliably sort on.

My question, finally, is how can I pre-filter or pre-convert the dates in my legacy content into machine-readable well-formatted date objects? Do I need to handwrite or parse with some other method these poorly formed dates into ISO dates? Can a plugin help me query on poorly formed date strings? Or is there another way for me to get the data out of my YML file when performing a query that I am not considering?

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [May 25, 2020, 8:29pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/2 "2020-05-25T20:29:31Z")

</div>

I see your problem.

According to liquid docs, jekyll will figure out your relatively clean “Mar 16, 2020” style date if you parse it through this filter:

```auto
{{ my_date | date: "%Y-%m-%d }}

```

Result `'2020-03-16'`

> **[date](https://shopify.github.io/liquid/filters/date/)**
>
> Liquid filter that prints and formats dates.

Unfortunately I can’t see a way to format the date and sort by it in one go.

Also by the way I’m seeing `sort` and sometimes `sort_by` so try those instead. I don’t see anyone using orderby.

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [May 25, 2020, 8:36pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/3 "2020-05-25T20:36:04Z")

</div>

I don’t know if you looked at docs or stackoverflow links but it helps to check first and post and research you found here

I found this while looking to sort by long date in jekyll.

You’re going to need to create a plugin in your repo and call it.

> <https://stackoverflow.com/questions/30933741/jekyll-cant-sort-collection-by-date>

The problem is the same as yours except they have date as d/m/y but here is the original answer copied.

```auto
# _plugins/filters.rb
module Jekyll
  module DateFilter
    require 'date'
    def date_sort(collection)
      collection.sort_by do |el|
        Date.parse(el.data['date'], '%d-%m-%Y')
      end
    end
  end
end
Liquid::Template.register_filter(Jekyll::DateFilter)

```

Used like so:

```auto
{% assign sortedResources = site.resources | date_sort | reverse %}
{% for resource in sortedResources %}
  <div>{{resource.title}}</div>

```

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [May 25, 2020, 8:47pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/4 "2020-05-25T20:47:34Z")

</div>

Your format will be

```auto
%B %-d, %Y

```

Based on [https://learn.cloudcannon.com/jekyll/date-formatting/](https://learn.cloudcannon.com/jekyll/date-formatting/)

If Mar and March inconsistency causes issues you can try use `%b` instead of `%B`.

Or do a find and replace in your IDE for ` - November` with `- Nov` for example. Note that going the other way will end up with NovNovember unless you are careful about adding trailing space to both sides.

---

<div class="post-metadata">

**Author:** ![jhogue](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhogue/32/1815_2.png) [@jhogue](https://talk.jekyllrb.com/u/jhogue)\
**Post date:** [May 26, 2020, 1:20am UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/5 "2020-05-26T01:20:30Z")

</div>

I did run into that StackOverflow question and tried it but I have no experience with custom filters. I ran into this error in the console as I have run into with your example:

```auto
Error: undefined method `data' for #<Hash:0x007f859f5e9d78>

```

I’m stuck here so that’s part of the reason why I decided to ask the question here… to see if I needed to learn how Ruby works so I can fix the filter or if there was another way.

Thanks for the thorough response.

---

<div class="post-metadata">

**Author:** ![jhogue](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhogue/32/1815_2.png) [@jhogue](https://talk.jekyllrb.com/u/jhogue)\
**Post date:** [May 29, 2020, 2:45pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/6 "2020-05-29T14:45:44Z")

</div>

Using this plugin, but it is being applied to a data YML file in Jekyll and I wonder if that is why I am getting the above error. Anyone know how I might change the plug in code to apply this to a data file instead of a collection?

```auto
module Jekyll
  module DateFilter
    require 'date'
    def date_sort(collection)
      collection.sort_by do |el|
        Date.parse(el.data['date'], '%b %-d %Y')
      end
    end
  end
end
Liquid::Template.register_filter(Jekyll::DateFilter)

```

The error again is:

```auto
Error: undefined method `data' for #<Hash:0x007f859f5e9d78>

```

Being applied this way:

```auto
{%- assign comments = site.data.comments | sort date_sort -%}

```

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [June 15, 2020, 10:00pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/7 "2020-06-15T22:00:11Z")

</div>

el is a hash so attributes are not using dot syntax.

Try

```auto
el['data']['date']

```

---

<div class="post-metadata">

**Author:** ![MichaelCurrin](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/michaelcurrin/32/2340_2.png) [@MichaelCurrin](https://talk.jekyllrb.com/u/MichaelCurrin)\
**Post date:** [June 15, 2020, 10:02pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/8 "2020-06-15T22:02:02Z")

</div>

Add debug lines before it

```auto
puts el
puts el.keys
puts el['data']

```

---

<div class="post-metadata">

**Author:** ![jhogue](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jhogue/32/1815_2.png) [@jhogue](https://talk.jekyllrb.com/u/jhogue)\
**Post date:** [June 25, 2020, 4:39pm UTC](https://talk.jekyllrb.com/t/how-to-sort-by-date-if-my-date-fields-are-not-formatted-as-dates/4465/9 "2020-06-25T16:39:15Z")

</div>

Thank you @MichaelCurrin, that was great.

In the end, since I am at the beginning of this project, I reformatted 200 or so dates to be in the `yyyy-mm-dd` format and that makes my Liquid sort work as intended. This plugin was an attempt to avoid reformatting legacy data, but I think in the long run it is in my best interest to start that process now.
