Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/evil-shrike/grunt-assemble
Browse files Browse the repository at this point in the history
…into evil-shrike-master
  • Loading branch information
doowb committed Sep 6, 2016
2 parents cb833ce + e6e1523 commit 0700e4a
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions lib/collection.js
Expand Up @@ -46,6 +46,27 @@ collection.update = function(col, page, context) {
return col;
};

function compare (a, b) {

if(a && b) {
if(a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
} else {
// pages without {sortby} field will at the end of collection
if (!a && !b) {
return 0;
}
if (!a) {
return 1;
}
return -1;
}
}

collection.sort = function(col) {
'use strict';
Expand All @@ -60,15 +81,7 @@ collection.sort = function(col) {

// sort items by the actual item
col.items.sort(function(a, b) {
if(a[col.inflection] && b[col.inflection]) {
if(a[col.inflection] < b[col.inflection]) {
return -1;
} else if (a[col.inflection] > b[col.inflection]) {
return 1;
} else {
return 0;
}
}
return compare(a[col.inflection], b[col.inflection]);
});

if(sortorder !== 'ASC') {
Expand All @@ -81,17 +94,7 @@ collection.sort = function(col) {
col.items.forEach(function(item) {

item.pages.sort(function(a, b) {
if(a.data[sortby] && b.data[sortby]) {
if(a.data[sortby] < b.data[sortby]) {
return -1;
} else if (a.data[sortby] > b.data[sortby]) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
return compare(a.data[sortby], b.data[sortby]);
});

if(sortorder !== 'ASC') {
Expand Down

0 comments on commit 0700e4a

Please sign in to comment.