0

regarding the code block below, i have a class that i use to create multiple objects, with each object containg some basic information about a particular car. i have created at the end, an array of these objects. how can i sort out this array of the objects based on a value like say, their weight, in ascending order from smallest to heaviest.

class raceCar {
    constructor(name, weight, lenght, width, height, horsePower, torque, colour){
        this._name = name;
        this._weight = weight;
        this._lenght = lenght;
        this._width = width;
        this._height = height;
        this._horsePower = horsePower;
        this._torque = torque;
        this._colour = colour;
    }

        get name(){
            return this._name;
        }

        get weight(){
            return this._weight;
        }

        get lenght(){
            return this._lenght;
        }

        get width(){
            return this._width;
        }

        get height(){
            return this._height;
        }

        get horsePower(){
            return this._horsePower;
        }

        get torque(){
            return this._torque
        }

        get colour(){
            return this._colour
        }

} 

const a45s = new raceCar ('a45s', 1550, 4445, 1850, 1412, 421, 500, 'black');
const m135i = new raceCar ('m135i', 1525, 4319, 1799, 1434, 306, 450, 'blue')
const golfR = new raceCar ('golfR', 1408, 4263, 1799, 1465, 310, 380, 'green')
const m240i = new raceCar ('240i', 1475, 4454, 1774, 1408, 340, 500, 'blue')
const cupra = new raceCar ('cupra', 1490, 4398, 1799, 1442, 300, 400, 'white')

var carArray = [a45s, m135i, golfR, m240i, cupra]
  • [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) – Pointy Apr 20 '21 at 18:38

2 Answers2

0

Just call the Array sort() method. like this:

carArray = carArray.sort((carA,carB) => carB.weight - carA.weight); // ascending order
carArray = carArray.sort((carA,carB) => carA.weight - carB.weight); // descending order
David B.
  • 695
  • 5
  • 10
0

Just use a sorting algorithm that you know, but now compare the objects' key values: carsArray[i].weight. On example of bubble sort il looks like:

for(let i = 0; i < carsArray.length; i++){
    for(let j = 0; j < carsArray.length - 1; j++){
          if(carsArray[j].weight < carsArray[j + 1].weight){
              swapTheObjects();
          } 
    } 
} 
Yura Shtefanko
  • 139
  • 1
  • 2
  • 6
  • 1
    hi thanks for your answer, i just need clarification on one or two things, sorry im kinda a beginner at this. i understand the first two for loops that you used to go through each value in the array. What i dont quite understand is the if statement. why does it say 'carsArray[j + 1].weight, what is the use of the + 1? Also am i right to assume that the swapTheObjects(); will be a method that i would have to write myself? – Prince RRory Apr 20 '21 at 18:55
  • It compares `j`-th element with the element that is his neighbor on the right(`j + 1`-th) and if `j`-th element has a bigger value than `j + 1`-th - you just need to put `j`-th element on the place of `j + 1`-th and `j + 1`-th on the place of `j`-th. For better understanding just watch video on YouTube about Bubble Sort, cause I won't be able to explain it so clear. – Yura Shtefanko Apr 20 '21 at 19:08