Guest User

Untitled

a guest
Apr 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <script>
  2. const companies = [
  3. {name: "Company One", category: "Finance", start: 1981, end: 2012},
  4. {name: "Company Two", category: "Retail", start: 1992, end: 2016},
  5. {name: "Company Three", category: "Auto", start: 1999, end: 2013},
  6. {name: "Company Four", category: "Technology", start: 1989, end: 2008},
  7. {name: "Company Five", category: "Finance", start: 2007, end: 2001},
  8. {name: "Company Six", category: "Auto", start: 1997, end: 2005},
  9. {name: "Company Seven", category: "Technology", start: 1987, end: 2003},
  10. {name: "Company Eight", category: "Retail", start: 1989, end: 2009},
  11. {name: "Company Nine", category: "Food", start: 1911, end: 2010}
  12. ]
  13.  
  14. const ages = [33, 25, 12, 20, 17, 34, 56, 87, 63, 8, 73];
  15.  
  16. // sort() old method
  17. const sortCompanies = companies.sort(function (a, b) {
  18. if (a.start > b.start) {
  19. return 1;
  20. } else {
  21. return -1;
  22. }
  23. });
  24.  
  25. // sort() with fat arrows
  26. const sortCompanies = companies.sort((a, b) => {
  27. return (a.start > b.start)
  28. });
  29.  
  30. const sortCompany = companies.sort((a, b) => (a.start > b.start));
  31.  
  32. const sortAges = ages.sort((a, b) => (a - b));
  33.  
  34. console.log(sortCompanies);
  35. console.log(sortCompany);
  36. console.log(sortAges);
  37.  
  38. // same output for sortCompanies and sortCompany
  39. {name: "Company Nine", category: "Food", start: 1911, end: 2010}
  40. {name: "Company One", category: "Finance", start: 1981, end: 2012}
  41. {name: "Company Seven", category: "Technology", start: 1987, end: 2003}
  42. {name: "Company Four", category: "Technology", start: 1989, end: 2008}
  43. {name: "Company Eight", category: "Retail", start: 1989, end: 2009}
  44. {name: "Company Two", category: "Retail", start: 1992, end: 2016}
  45. {name: "Company Six", category: "Auto", start: 1997, end: 2005}
  46. {name: "Company Three", category: "Auto", start: 1999, end: 2013}
  47. {name: "Company Five", category: "Finance", start: 2007, end: 2001}
  48.  
  49. // sortAges
  50. [8, 12, 17, 20, 25, 33, 34, 56, 63, 73, 87]
  51.  
  52. </script>
Add Comment
Please, Sign In to add comment