2016-10-01 19:25:32 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
exports.sortAreasOrConfs = sortAreasOrConfs;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Method for sorting message, file, etc. areas and confs
|
|
|
|
// If the sort key is present and is a number, sort in numerical order;
|
|
|
|
// Otherwise, use a locale comparison on the sort key or name as a fallback
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2016-10-01 19:25:32 +00:00
|
|
|
function sortAreasOrConfs(areasOrConfs, type) {
|
2018-06-22 05:15:04 +00:00
|
|
|
let entryA;
|
|
|
|
let entryB;
|
2016-10-01 19:25:32 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
areasOrConfs.sort((a, b) => {
|
|
|
|
entryA = type ? a[type] : a;
|
|
|
|
entryB = type ? b[type] : b;
|
2016-10-01 19:25:32 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(_.isNumber(entryA.sort) && _.isNumber(entryB.sort)) {
|
|
|
|
return entryA.sort - entryB.sort;
|
|
|
|
} else {
|
|
|
|
const keyA = entryA.sort ? entryA.sort.toString() : entryA.name;
|
|
|
|
const keyB = entryB.sort ? entryB.sort.toString() : entryB.name;
|
|
|
|
return keyA.localeCompare(keyB, { sensitivity : false, numeric : true } ); // "natural" compare
|
|
|
|
}
|
|
|
|
});
|
2016-10-01 19:25:32 +00:00
|
|
|
}
|