JSFiddle - React, Tailwind, and code Playground
JavaScript
var cars = ["Ferrari", "Lamborghini", "Jaguar", "Hummer", "Toyota"],
price = [12, 34.5, 3.54, 45.9, 3.44],
result,
joinCarPrices = function () {
var index = 0,
carPrices = [];
for (index = 0; index < cars.length; index++) {
carPrices[index] = {
'car': cars[index],
'price': price[index]
};
}
return carPrices;
},
priceSort = function (a, b) {
// If the first car is less than the second car
if (a.price < b.price) {
return 1;
} else if (a.price > b.price) {
// If the first car is more than the second car
return -1
} else {
// Else sort by the car name
return a.car < b.car ? -1 : 1;
}
};
cars = joinCarPrices(); // Join the Cars/Prices together as Objects, into an array
result = cars.sort(priceSort); // Sort the Cars based on the Price Sort function
result = result.slice(0, 3); // Slice to only give us array items 0-3
console.log(result);