daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sortByKeys(arr, ...keys) {
  2.   // sort arr by keys
  3.   // keys can be:
  4.   // 1. property name
  5.   // 2. -property name
  6.   // 3. function arr object => value
  7.   let keyToInt = (e1,e2,k, factor) => {
  8.     let v1 = k(e1);
  9.     let v2 = k(e2) ;
  10.     return v1>v2 ? 1*factor : (v1==v2 ? 0 : -1*factor)
  11.     };
  12.     let keysToInt = (e1, e2, keys) => {
  13.         for (let k of keys) {
  14.             let kPred = null
  15.             let factor = 1;
  16.             if (typeof k == 'string') {
  17.                 if (k[0] == '-') {
  18.                     kPred = e => e[k.substr(1)];
  19.                     factor = -1;
  20.                 } else {
  21.             kPred = e => e[k];
  22.                 }
  23.             } else {
  24.                 kPred = k
  25.             }
  26.             let res = keyToInt(e1,e2,kPred, factor);
  27.             if (res !=0) {
  28.                 return res;
  29.             }
  30.         }
  31.         return 0;
  32.     }
  33.     return arr.sort((e1,e2) => keysToInt(e1,e2,keys));
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top