Skip to content

Commit

Permalink
ABE-167: sort feature server side
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Aug 27, 2016
1 parent 4b39b60 commit c371857
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/cli/helpers/file-parser.js
Expand Up @@ -359,6 +359,41 @@ export default class FileParser {
return res
}

/**
* This function makes sorting on an array of Json objects possible.
* Pass the property to be sorted on.
* Usage: myArray.sort(FileParser.predicatBy('date',-1));
* @param String prop the json property to sort on
* @param integer order order ASC if 1, DESC if -1
* @return integer the ordered value
*/
static predicatBy(prop, order){
if (order !== -1) {
order = 1;
}
if(prop === 'date'){
return function(a,b){
a = new Date(a[prop]);
b = new Date(b[prop]);
if( a > b){
return 1*order;
}else if( a < b ){
return -1*order;
}
return 0;
}
}

return function(a,b){
if( a[prop] > b[prop]){
return 1*order;
}else if( a[prop] < b[prop] ){
return -1*order;
}
return 0;
}
}

static getAllFiles() {
var site = folderUtils.folderInfos(config.root)
var allDraft = []
Expand Down
5 changes: 5 additions & 0 deletions src/cli/models/Manager.js
Expand Up @@ -11,6 +11,10 @@ class Manager {
if(enforcer != singletonEnforcer) throw "Cannot construct Json singleton"

this._list = FileParser.getAllFiles();
this._list[0].files.sort(FileParser.predicatBy('date',-1));
// this._list.forEach(function(elt){
// console.log(elt)
// })
}

static get instance() {
Expand All @@ -28,6 +32,7 @@ class Manager {
updateList() {

this._list = FileParser.getAllFiles();
this._list.sort(FileParser.predicatBy('date'));

return this
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/public/scripts/template-engine.js
Expand Up @@ -39,7 +39,7 @@ class Engine {
this.table = null
$(document).ready(() => {
this.table = $('#navigation-list').DataTable({
"order": [[ 3, 'desc' ]],
//"order": [[ 3, 'desc' ]],
"pageLength": 50,
"autoWidth": false
})
Expand Down

0 comments on commit c371857

Please sign in to comment.