Hey guys so im a highschool student and I just started to get into coding. I have this project that we are doing in class were I have to design a page then write a sort function to sort a list of cars prices from least to greatest and greatest to least. I have managed to get the sorting correct with the numbers but I can't figure out how to relate the prices with the title of the car that I am sorting so that I can move the title with the price. Does anyone know how I can accomplish this?
Here's my code:
onEvent("sortDropDown", "change", function( ) {
var sortDropDown = getText("sortDropDown");
var prices = [(getNumber("carPrice0")), (getNumber("carPrice1")), (getNumber("carPrice2"))];
var i = 0;
if (sortDropDown === "Price: Greatest to Least") {
descendingOrder(prices);
for (i = 0; i < 3; i++) {
setProperty("carPrice" + [i], "text", prices[i]);
}
} else if ((sortDropDown === "Price: Least to Greatest")) {
numericalOrder(prices);
for (i = 0; i < 3; i++) {
setProperty("carPrice" + [i], "text", prices[i]);
}
} else {
setCar(currentCarType);
}
});
function numericalOrder(list) {
var holder = 0;
for (var i = 0; i < list.length; i++) {
for (var j = i; j < list.length; j++) {
if (list[j] < list[i]) {
holder = list[j];
list[j] = list[i];
list[i] = holder;
}
}
}
return list;
}
function descendingOrder(list) {
var holder = 0;
for (var i = 0; i < list.length; i++) {
for (var j = i; j < list.length; j++) {
if (list[j] > list[i]) {
holder = list[j];
list[j] = list[i];
list[i] = holder;
}
}
}
return list;
}