我写了一些相当简单的代码来对Javascript中的对象数组进行排序:
const inventors = [
{ first: "Albert", last: "Einstein", born: 1879, passed: 1955 },
{ first: "Isaac", last: "Newton", born: 1643, passed: 1727 },
{ first: "Galileo", last: "Galilei", born: 1564, passed: 1642 },
{ first: "Marie", last: "Curie", born: 1867, passed: 1934 },
{ first: "Johannes", last: "Kepler", born: 1571, passed: 1630 },
{ first: "Nicolaus", last: "Copernicus", born: 1473, passed: 1543 },
{ first: "Max", last: "Planck", born: 1858, passed: 1947 },
{ first: "Katherine", last: "Blodgett", born: 1898, passed: 1979 },
{ first: "Ada", last: "Lovelace", born: 1815, passed: 1852 },
{ first: "Sarah E.", last: "Goode", born: 1855, passed: 1905 },
{ first: "Lise", last: "Meitner", born: 1878, passed: 1968 },
{ first: "Hanna", last: "Hammarström", born: 1829, passed: 1909 }
];
console.log(inventors);
const sortedByLifespan = inventors.sort(
(inv1, inv2) => inv1.passed - inv1.born - inv2.passed - inv2.born
);
由于我在执行任何操作之前先记录数组 ,所以我希望它以我定义的顺序记录刚创建的数组。但是,由于某些原因,会记录已排序的数组。这是怎么回事?