Quick Tech Tip – How to use the Array.prototype.sort() JavaScript method

We can use the Array.prototype.sort() method to return the scientist’s birth date and sort it by descending order (oldest to youngest).

Let’s take a look at an example…

Here’s an array of scientist data with first name, last name, date of birth and date of passing on:

const scientists = [
{ first: ‘Isaac’, last: ‘Newton’, year: 1643, passed: 1727 },
{ first: ‘Galileo’, last: ‘Galilei’, year: 1564, passed: 1642 },
{ first: ‘Marie’, last: ‘Curie’, year: 1867, passed: 1934 },
{ first: ‘Johannes’, last: ‘Kepler’, year: 1571, passed: 1630 },
{ first: ‘Nicolaus’, last: ‘Copernicus’, year: 1473, passed: 1543 },
{ first: ‘Max’, last: ‘Planck’, year: 1858, passed: 1947 },
{ first: ‘Sarah E.’, last: ‘Goode’, year: 1855, passed: 1905 },
{ first: ‘Lise’, last: ‘Meitner’, year: 1878, passed: 1968 },
{ first: ‘Albert’, last: ‘Einstein’, year: 1879, passed: 1955 },
];
array sort() method
and output the result in table format to the console:
JavaScript array sort() method result

That concludes today’s tech tip.