This is designed with [assemble][] collections in mind, but it's generic enough that it should work with an array of strings too.
Example
The following example shows how to sort and filter the items in an [assemble][] collection.
var assemble = require('assemble');
var app = assemble();
app.helper('sortItems', require('{%= name %}'));
app.pages('templates/*.hbs');Given that four pages have been loaded: foo.hbs, bar.hbs, baz.hbs, and some-file.hbs, the following:
When rendered with these options:
var locals = {order: ['baz', 'bar', 'foo']};
app.render('some-file.hbs', locals, function(err, view) {
if (err) {
console.log(err);
process.exit(1);
}
console.log(view.contents.toString());
});Would result in:
baz.hbs
bar.hbs
foo.hbs
You can also pass the sort order as the last argument as a comma-separated string:
Or when used as a [handlebars][] helper, you can pass the sort order on the options hash as a comma-separated string: