运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Stable Sort</h1> <p>From ES2019, browsers must use a stable sorting algorithm.</p> <p>When sorting elements on a key, the elements must keep their relative position to other objects with the same key.</p> <p id="demo"></p> <script> const myArr = [ {name:"X00",price:100 }, {name:"X01",price:100 }, {name:"X02",price:100 }, {name:"X03",price:100 }, {name:"X04",price:110 }, {name:"X05",price:110 }, {name:"X06",price:110 }, {name:"X07",price:110 }, {name:"X08",price:120 }, {name:"X09",price:120 }, {name:"X10",price:120 }, {name:"X11",price:120 }, {name:"X12",price:130 }, {name:"X13",price:130 }, {name:"X14",price:130 }, {name:"X15",price:130 }, {name:"X16",price:140 }, {name:"X17",price:140 }, {name:"X18",price:140 }, {name:"X19",price:140 } ]; myArr.sort( (p1, p2) => { if (p1.price < p2.price) return -1; if (p1.price > p2.price) return 1; return 0; }); let txt = ""; myArr.forEach(myFunction); function myFunction(value) { txt += value.name + " " + value.price + "<br>"; } document.getElementById("demo").innerHTML = txt; </script> </body> </html>