1

I have this piece of code that executes a sort for this kind of structure:

items:
 0:
  date: '2018-01-02'
 1:
  date: '2019-02-02'
 2: 
  date: '2016-03-04'

Sort:

{% assign sorted = items.sort {|x,y| y[1].date <=> x[1].date} %}

Based on: https://stackoverflow.com/a/5710429/2797942

When I run jekyll serve it returns Liquid Warning: Liquid syntax error (line 18): Unexpected character { in "{{items.sort {|x,y| y[1].date <=> x[1].date} }}

I'm quite new to Ruby and Jekyll, can someone help me out?

jhnferraris
  • 1,361
  • 1
  • 12
  • 34

1 Answers1

0

Jekyll templates can't accept full Ruby. You can output a context variable, optionally modified by a Liquid filter; invoking sort method and passing a Ruby block is out of the question. Try instead:

{{ items | sort: "date" | reverse }}
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Looks like I can't do this in Jekyll since it's a hash. I have to do this in plugin. Any thoughts on how will approach this in a plugin? – jhnferraris May 30 '19 at 05:02
  • 1
    Then it seems it's a duplicate, the linked question has several techniques mentioned. Note that (AFAIK) GitHub pages (which is like the most common use-case for Jekyll) does not support user-written plugins, so if you're planning to publish on GH pages, a plugin would not help you. – Amadan May 30 '19 at 05:07