I'm trying to bubble sort by price and insert sort by name but I'm confused about how to print the sorted list. Do I just console.log because I tried that and it just printed the array but without it being sorted? Am I supposed to return something? (pls don't mind the formatting I'm new here sorry!)
//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;
}
Any help is really appreciated, thank you!