noximus
10/16/2018 - 1:33 AM

.sort()

Javascript ES6 .sort(); with shorthand

const companies = {
  {name: "Company One", category: "Finance", start: 1981, end: 2003},
  {name: "Company One", category: "Finance", start: 1992, end: 2008},
  {name: "Company One", category: "Finance", start: 1999, end: 2007},
  {name: "Company One", category: "Finance", start: 1989, end: 2010},
  {name: "Company One", category: "Finance", start: 2009, end: 2014},
  {name: "Company One", category: "Finance", start: 1987, end: 2010},
  {name: "Company One", category: "Finance", start: 1986, end: 1996},
  {name: "Company One", category: "Finance", start: 2011, end: 2016},
  {name: "Company One", category: "Finance", start: 1981, end: 1989}
}
 
const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];

// map 

// sort companies by start year
const sortCompanies = companies.sort(function(c1, c2) {
  if(c1.start > c2.start) {
    return 1;
  } else {
    return -1;
  }
});

const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? : -1));

// sort ages
const sortAges = ages.sort((a, b) => a - b);

const ageSum = ages.reduce(function(total, age ) {
  return total + age;
}, 0);

const ageSum = agesreduce((total, age) => total + age, 0);

const.log(ageSum);

// Get total years for all companies

const totalYears = companies.reduce(function(total, company) {
  return total + (company.end - company.start);
})

console.log(ageSum);