Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- const companies = [
- {name: "Company One", category: "Finance", start: 1981, end: 2012},
- {name: "Company Two", category: "Retail", start: 1992, end: 2016},
- {name: "Company Three", category: "Auto", start: 1999, end: 2013},
- {name: "Company Four", category: "Technology", start: 1989, end: 2008},
- {name: "Company Five", category: "Finance", start: 2007, end: 2001},
- {name: "Company Six", category: "Auto", start: 1997, end: 2005},
- {name: "Company Seven", category: "Technology", start: 1987, end: 2003},
- {name: "Company Eight", category: "Retail", start: 1989, end: 2009},
- {name: "Company Nine", category: "Food", start: 1911, end: 2010}
- ]
- const ages = [33, 25, 12, 20, 17, 34, 56, 87, 63, 8, 73];
- // sort() old method
- const sortCompanies = companies.sort(function (a, b) {
- if (a.start > b.start) {
- return 1;
- } else {
- return -1;
- }
- });
- // sort() with fat arrows
- const sortCompanies = companies.sort((a, b) => {
- return (a.start > b.start)
- });
- const sortCompany = companies.sort((a, b) => (a.start > b.start));
- const sortAges = ages.sort((a, b) => (a - b));
- console.log(sortCompanies);
- console.log(sortCompany);
- console.log(sortAges);
- // same output for sortCompanies and sortCompany
- {name: "Company Nine", category: "Food", start: 1911, end: 2010}
- {name: "Company One", category: "Finance", start: 1981, end: 2012}
- {name: "Company Seven", category: "Technology", start: 1987, end: 2003}
- {name: "Company Four", category: "Technology", start: 1989, end: 2008}
- {name: "Company Eight", category: "Retail", start: 1989, end: 2009}
- {name: "Company Two", category: "Retail", start: 1992, end: 2016}
- {name: "Company Six", category: "Auto", start: 1997, end: 2005}
- {name: "Company Three", category: "Auto", start: 1999, end: 2013}
- {name: "Company Five", category: "Finance", start: 2007, end: 2001}
- // sortAges
- [8, 12, 17, 20, 25, 33, 34, 56, 63, 73, 87]
- </script>
Add Comment
Please, Sign In to add comment