liquid
liquid copied to clipboard
Add `sort_numeric` filter
This PR adds a sort_numeric filter. See also https://github.com/Shopify/liquid/pull/1028 and https://github.com/Shopify/liquid/issues/980.
sort_numeric sorts an array by numerical values extracted from runs of digits within the string representation of each item.
require "liquid"
template = Liquid::Template.parse("{{ array | sort_numeric | join }}")
puts template.render({ "array" => ["v1.2", "v1.9", "v10.0", "v0.1", "v1.10", "v1.1.0"] })
# v0.1 v1.1.0 v1.2 v1.9 v1.10 v10.0
puts template.render({ "array" => ["107", "042", "0001", "02", "17"] })
# 0001 02 17 042 107
puts template.render({ "array" => ["10.1", "3.5", "2.3", "1.1", "1.01"] })
# 1.01 1.1 2.3 3.5 10.1
Please somebody push this please god