Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Sort collection by last_modified_at #70

Open
lsarni opened this issue Apr 23, 2019 · 6 comments · May be fixed by #80
Open

Sort collection by last_modified_at #70

lsarni opened this issue Apr 23, 2019 · 6 comments · May be fixed by #80

Comments

@lsarni
Copy link

lsarni commented Apr 23, 2019

I'm trying to use this value to sort a custom collection called docs.

{% assign sorted_docs = site.docs | sort: 'last_modified_at' | reverse %}
This causes:

Liquid Exception: Liquid error (line 6): comparison of Array with Array failed in index.md
Error: Liquid error (line 6): comparison of Array with Array failed
Error: Run jekyll build --trace for more information.

The same happens if I remove the reverse. But it doesn't happen if I use other field to sort it (like title).

Since it isn't documented I'm not sure if this can be used for sorting, should it work?

@xplosionmind
Copy link

If it’s not a bug, it should definitely be a feature request! This happens to me too. It would be so useful to sort collections based on the last modification date and time.

This is the error message I get (my collection is called "Jar")

Liquid Warning: Liquid syntax error (line 87): Expected end_of_string but found pipe in "note in site.jar | sort: 'last_modified_at'" in home.html

Thanks a lot, this plugin is great!

@douo douo linked a pull request Jun 11, 2020 that will close this issue
@henryiii
Copy link

henryiii commented Apr 29, 2021

I've worked around this by adding a plugin that adds a string version, since that can be compared.

Real comparisons would be even better!

# frozen_string_literal: true

require 'fileutils'
require 'pathname'
require 'jekyll-last-modified-at'

module Recents
  # Generate change information for all markdown pages
  class Generator < Jekyll::Generator
    def generate(site)
      items = site.pages.select { |p| p.path.end_with? '.md' }
      items.each do |page|
        page.data['last_modified_at_str'] = Jekyll::LastModifiedAt::Determinator.new(site.source, page.path, '%FT%T%:z').to_s
      end
    end
  end
end

@xplosionmind
Copy link

Hi @henryiii! I am no Ruby developer, but I am wondering how I could include not only .md, but also .html files in your plugin.

@henryiii
Copy link

@xplosionmind,

String#end_with? takes multiple arguments. So just change this line:

items = site.pages.select { |p| p.path.end_with?('.md', '.html') }

@xplosionmind
Copy link

Thanks a lot!

T

@heyteresagreen
Copy link

heyteresagreen commented Jan 17, 2023

Hi, I'm trying to display a list of notes ordered by most recently modified. I'm using this line:
{% assign notes = site.notes | sort: 'last_modified_at' | reverse %}

It's not creating an error as in the above posts, and it does seem to be sorting the notes based on something, but it doesn't seem to be the last modified date. In my list it shows the note title and {{ note.last_modified_at }} which seems to be correct, but it isn't sorted in that order.

Is the issue my implementation, or just that the plugin isn't set up to do this?

Here's my full template if that helps:

{% assign notes = site.notes | sort: 'last_modified_at' | reverse %}

  {% for note in notes %}
  <p>
      <a href="{{ note.url }}" class="internal-link">{{ note.title }}</a> 
      <small>Updated {{ note.last_modified_at | date: "%B %-d, %Y" }}</small>
  </p>
  {% endfor %}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants