Advertisement
Guest User

Untitled

a guest
Mar 28th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. function emailSort(a, b) {
  2. const importance = {"gmail.com": 1, "yahoo.com": 2, "rocketmail.com": 3, "ymail.com": 4};
  3.  
  4. const importanceOfA = importance[a.split('@')[1]];
  5. const importanceOfB = importance[b.split('@')[1]];
  6.  
  7. if (importanceOfA && !importanceOfB) return -1;
  8. if (importanceOfB && !importanceOfA) return 1;
  9. if (importanceOfA && importanceOfB) return importanceOfA - importanceOfB;
  10.  
  11. return 0;
  12. }
  13.  
  14. arra = [
  15. "juni1@rocketmail.com",
  16. "juni2@rocketmail.com",
  17. "juni3@rocketmail.com",
  18. "juni1@yahoo.com",
  19. "juni2@yahoo.com",
  20. "juni3@yahoo.com",
  21. "juni1@ymail.com",
  22. "juni3@ymail.com",
  23. "juni2@ymail.com",
  24. "juni1@gmail.com",
  25. "junaid4567@gmail.com",
  26. "juni4@gmail.com",
  27. ]
  28. newArray = arra.sort(emailSort);
  29. console.log(newArray)
  30. //will return
  31. // [ 'juni4@gmail.com', 
  32. // 'junaid4567@gmail.com', 
  33. // 'juni1@gmail.com', 
  34. // 'juni1@yahoo.com', 
  35. // 'juni2@yahoo.com', 
  36. // 'juni3@yahoo.com', 
  37. // 'juni2@rocketmail.com', 
  38. // 'juni3@rocketmail.com', 
  39. // 'juni1@rocketmail.com', 
  40. // 'juni2@ymail.com', 
  41. // 'juni3@ymail.com', 
  42. // 'juni1@ymail.com' ] 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement