提问人:Pranjal 提问时间:12/3/2018 最后编辑:hannad rehmanPranjal 更新时间:12/3/2018 访问量:54
Javascript 两个 arraySort 返回相同的结果
Javascript two arraySort returning same result
问:
你好,我是javascript的新手。在学习数组时,我在排序方面遇到了问题。首先,我根据他们的出生日期进行排序,然后根据他们的年龄进行排序。我仍然得到相同的结果。请帮忙。
法典:
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1711},
{ 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: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];
const firstSort= inventors.sort((a,b) => a.year > b.year? -1 : 1 );
console.log(firstSort);
const secondSort = inventor.sort((a,b) => (a.year-a.passed > b.year-b.passed) ? -1 : 1);
console.log(secondSort);
这是第一个输出:
0: Object { first: "Ada", last: "Lovelace", year: 1815, … }
1: Object { first: "Sarah E.", last: "Goode", year: 1855, … }
2: Object { first: "Johannes", last: "Kepler", year: 1571, … }
3: Object { first: "Marie", last: "Curie", year: 1867, … }
4: Object { first: "Isaac", last: "Newton", year: 1643, … }
5: Object { first: "Nicolaus", last: "Copernicus", year: 1473, … }
6: Object { first: "Albert", last: "Einstein", year: 1879, … }
7: Object { first: "Galileo", last: "Galilei", year: 1564, … }
8: Object { first: "Hanna", last: "Hammarström", year: 1829, … }
9: Object { first: "Katherine", last: "Blodgett", year: 1898, … }
10: Object { first: "Max", last: "Planck", year: 1858, … }
11: Object { first: "Lise", last: "Meitner", year: 1878, … }
这是第二个输出
0: Object { first: "Ada", last: "Lovelace", year: 1815, … }
1: Object { first: "Sarah E.", last: "Goode", year: 1855, … }
2: Object { first: "Johannes", last: "Kepler", year: 1571, … }
3: Object { first: "Marie", last: "Curie", year: 1867, … }
4: Object { first: "Isaac", last: "Newton", year: 1643, … }
5: Object { first: "Nicolaus", last: "Copernicus", year: 1473, … }
6: Object { first: "Albert", last: "Einstein", year: 1879, … }
7: Object { first: "Galileo", last: "Galilei", year: 1564, … }
8: Object { first: "Hanna", last: "Hammarström", year: 1829, … }
9: Object { first: "Katherine", last: "Blodgett", year: 1898, … }
10: Object { first: "Max", last: "Planck", year: 1858, … }
11: Object { first: "Lise", last: "Meitner", year: 1878, … }
答:
1赞
vrintle
12/3/2018
#1
Array.sort() 对返回值起作用。
此外,寿命期的计算应大于a.passed - a.year
a.passed
a.year
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1711},
{ 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: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];
const firstSort = inventors.sort((a,b) => a.year - b.year); // exactly the same as a.year > b.year ? 1 : -1;
console.log(firstSort);
const secondSort = inventors.sort((a,b) => (a.passed - a.year) - (b.passed - b.year));
console.log(secondSort);
评论
1赞
Keith
12/3/2018
Math.sort
??, 我假设你的意思Array.prototype.sort
0赞
vrintle
12/3/2018
@Keith谢谢。有时我想的不是我写的。
0赞
Pranjal
12/4/2018
关于那个“过去了一年”。我把它改成了过去 - 年。但这两个变量的结果相同。2 3 小时后,出于某种奇怪的原因,我发现我的代码可以在 google chrome 中工作,并且在 mozilla firefox 中给出了这种错误的答案。这是什么错误。奇怪 :|
0赞
Learner
12/3/2018
#2
您可以根据要获得所需结果的顺序更改条件。
首先,您需要按年份排序,然后可以通过减去传递和年份属性对排序数组进行排序并对其进行排序。
我希望下面的代码能解决您的问题。
const inventors = [
{ first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
{ first: 'Isaac', last: 'Newton', year: 1643, passed: 1711},
{ 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: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
{ first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
{ first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
{ first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
{ first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
];
let sortedArr = inventors.sort((a,b) =>(a.year > b.year) ? 1 : ((b.year > a.year) ? -1 : 0)).sort((a,b) => (a.year - a.passed > b.year - b.passed) ? 1 : (b.year - b.passed > a.year - a.passed) ? -1 : 0 )
console.log(sortedArr)
评论
inventors.slice(0).sort
console.log(JSON.stringify(firstSort));
sorting according to their age
请注意,你不能只用年数来计算一个人的年龄。:)