als-store
Version:
Library for streamlined file management and advanced data caching, featuring intelligent file searching, dynamic cache control, and flexible file operations
28 lines (26 loc) • 917 B
JavaScript
function sortFn(result, property, desc = false) {
if (result.length === 0 || !property) return result;
if (property.startsWith('-')) {
desc = true
property = property.slice(1, property.length)
}
function findValue(obj) {
if (property === 'name') return obj.name
if (property.startsWith('stats.') && obj.stats) {
const [k1, k2] = property.split('.')
return obj.stats[k2]
} else if (property.startsWith('params.')) {
const [k1, k2] = property.split('.')
return obj.params[k2]
}
}
result.sort((a, b) => {
let valueA = findValue(a) || '';
let valueB = findValue(b) || '';
if (typeof valueA === 'string' && typeof valueB === 'string') return valueA.localeCompare(valueB)
else return valueA - valueB;
});
if (desc) result.reverse()
return result
}
module.exports = sortFn