Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const weight = x => x.split('').reduce((p,c) => p + +c, 0);
  2. const compare = (a, b) => a.weight == b.weight ? a.value > b.value : a.weight - b.weight;
  3.  
  4. const orderWeight = str => str.trim()
  5.     .split(' ')
  6.     .map(x => ({value: x, weight: weight(x)}))
  7.     .sort(compare)
  8.     .reduce((p,c) => `${p} ${c.value}`,'')
  9.     .trim();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement