+ sort.js utils

This commit is contained in:
tmshv committed 2016-02-24 20:52:15 +03:00
1 parent ca415e58de
commit df12d1bf0d
1 file changed
+15
+15
View File
@@ -0,0 +1,15 @@
export function sortByPattern(list, pattern, map) {
let weight = i => {
const index = pattern.indexOf(map(i));
return index > -1 ? pattern.length - index : 0;
};
return sortByWeight(list, weight)
}
export function sortByWeight(list, fn) {
return list
.map(i => [fn(i), i])
.sort((a, b) => b[0] - a[0])
.map(i => i[1]);
}