JavaScript Bubble & Insert 对数组中的对象进行排序



我试图按价格冒泡排序并按名称插入排序,但我对如何打印排序列表感到困惑。我只是console.log吗?因为我试过了,它只是打印了数组,但没有对它进行排序?我应该退货吗?(请不要介意格式,对不起,我是新来的!(

//Class 
class Shoe{
constructor(name, price, type) {
this.name = name;
this.price = price;
this.type = type;
}
}
// list of objects
var shoes = [
new Shoe('Nike AirMax 90', '120', 'Casual'),
new Shoe('Jordan Retro 1', '110', 'Casual'),
new Shoe('Jadon Doc Martens', '250', 'Seasonal boots'),
new Shoe('Adidas X Ghosted', '110', 'Athletic'),
new Shoe('Nike Vapourmax Flyknit', '250', 'Casual'),
new Shoe('Aldo Loafers', '130', 'Formal'),
new Shoe('Timberlands', '199', 'Seasonal boots'),
new Shoe('Converse High Tops', '70', 'Casual'),
new Shoe('Converse Low Tops', '80', 'Casual'),
new Shoe('Adidas NMDs', '110', 'Athletic'),
new Shoe('Heels', '130', 'Formal'),
new Shoe('Nike AirForce', '150', 'Casual')
];
// bubble sort
function bubbleSort(shoes) {
var swapped;
do {
swapped = false;
for (var i=0; i < shoes.length-1; i++) {
if (shoes[i].price > shoes[i+1].price) {
var temp = shoes[i];
shoes[i] = shoes[i+1];
shoes[i+1] = temp;
swapped = true;
}
}
} while (swapped);
}
// insertion sort
function insertionSort(shoes) {
let a = shoes.length;
for (let i = 1; i < a; i++) {
// Choosing the first element in our unsorted subarray
let first = shoes[i];
// The last element of our sorted subarray
let l = i-1; 
while ((l > -1) && (first.type < shoes[l].type)) {
shoes[l+1] = shoes[l];
l--;
}
shoes[l+1] = first;
}
return shoes;
}

非常感谢您的帮助,谢谢!

排序函数可以通过改变输入来对输入数据进行排序,也可以通过获取并返回排序后的副本来对原始数据进行排序。任何一种类型都是可以接受的做事方式。

在您的示例中,两种排序都会对传入的数据进行变异,因此,尽管它们可以方便地返回数组(就像您的插入排序已经做的那样(,但请记住,无论是否记录了返回值,原始数组仍会更改。

然而,我可以看到,你在这里遇到的一个问题实际上是关于字符串转换的——你需要将价格转换为数字才能正确地进行比较。目前,您正在执行字符串比较。

有很多方法可以做到这一点,但我个人喜欢一元加号。

class Shoe {
constructor(name, price, type) {
this.name = name;
this.price = price;
this.type = type;
}
}
var shoes = [
new Shoe('Nike AirMax 90', '120', 'Casual'),
new Shoe('Jordan Retro 1', '110', 'Casual'),
new Shoe('Jadon Doc Martens', '250', 'Seasonal boots'),
new Shoe('Adidas X Ghosted', '110', 'Athletic'),
new Shoe('Nike Vapourmax Flyknit', '250', 'Casual'),
new Shoe('Aldo Loafers', '130', 'Formal'),
new Shoe('Timberlands', '199', 'Seasonal boots'),
new Shoe('Converse High Tops', '70', 'Casual'),
new Shoe('Converse Low Tops', '80', 'Casual'),
new Shoe('Adidas NMDs', '110', 'Athletic'),
new Shoe('Heels', '130', 'Formal'),
new Shoe('Nike AirForce', '150', 'Casual')
];
function bubbleSort(shoes) {
var swapped;
do {
swapped = false;
for (var i = 0; i < shoes.length - 1; i++) {

// Must convert prices to numbers,
// otherwise they get compared as strings
if (+shoes[i].price > +shoes[i + 1].price) {
var temp = shoes[i];
shoes[i] = shoes[i + 1];
shoes[i + 1] = temp;
swapped = true;
}
}
} while (swapped);
return shoes;
}
bubbleSort(shoes);
console.log('Bubble Sort:n', shoes);

不确定从头开始编写排序的原因。不要误解我的意思,我喜欢在很多事情上"滚自己的",但如果你想做一种你可以做的事情:

shoes.sort(function (a, b) {return a.price - b.price});

至于显示结果,我通常只制作一个带有div元素和按钮的简单html页面。当我点击按钮时,我会让我的函数使用div的innerText属性填充div,并得到结果。请记住,当向显示器发送对象或数组时,您可能希望使用JSON.stringify,以便获得实际内容,而不仅仅是

[对象对象],[对象对象][对象对象对象]

最新更新