Advertisement
Guest User

Test

a guest
May 11th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. function galactic(arr) {
  2. let galacticMap = new Map();
  3. for (let obj of arr) {
  4. let system = obj.system; let candidate = obj.candidate; let votes = Number(obj.votes);
  5.  
  6. if(!galacticMap.has(system)){
  7. galacticMap.set(system,new Map());
  8. }
  9.  
  10. if (!galacticMap.get(system).has(candidate)) {
  11. galacticMap.get(system).set(candidate, 0);
  12. }
  13.  
  14. let oldValue = galacticMap.get(system).get(candidate);
  15. galacticMap.get(system).set(candidate,oldValue + votes);
  16. }
  17.  
  18. let sorted = [...galacticMap].sort(mySort);
  19.  
  20. //Тука пробвам с value и не става, а ако е value() със скобите гърми...
  21. function mySort(a,b) {
  22. return b[1].value - a[1].value;
  23. }
  24.  
  25. //Тука и с мапа
  26. function mySort(a,b) {
  27. return galacticMap.get(b[1]) - galacticMap.get(a[1]);
  28. }
  29.  
  30. //И така пробвах и пак не става
  31. let sorted = [...galacticMap].sort((a,b) => otherSort(galacticMap,a,b));
  32. function otherSort(map,a,b) {
  33. return map.get(b[1]) - map.get(a[1]);
  34. }
  35.  
  36. //И така ъпсурт
  37. let sorted = [...galacticMap].sort(anotherSort);
  38. function anotherSort(a,b) {
  39. return b[1][1] - a[1][1];
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement