Search code examples
javascriptarraysobject

Arrange array according to object property


Morning!

I made the function in order to organize the array according to the property that the user passes by parameter. But don't run, why?

I want sort by some parameter that exists in the object, like ordering by "wheel" or "id", for example.

const data = [
    {
         "color": "Blue",
         "door": 3,
         "wheel": 3,
         "year": 2005,
         "brand": "GMC",
         "sold": false,
         "owner": "Chalmers Boobyer",
         "motor": 1.7,
         "assembled": "09/08/2022"
     },
     {
         "color": "Indigo",
         "door": 4,
         "wheel": 4,
         "year": 1996,
         "brand": "Lincoln",
         "sold": true,
         "owner": "Morten Coffin",
         "motor": 1.7,
         "assembled": "26/08/2021"
     }
];


function bubbleSort(arr, ord='asc', property){
    if(ord === 'asc'){
        for(let i = 0; i < arr.length; i++){
            for(let j = 0; j < arr.length; j++){
                if(arr[j].property > arr[j+1].property){
                    let temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    } else if(ord === 'desc'){
        for(let i = 0; i < arr.length; i++){
            for(let j = 0; j < arr.length; j++){
                if(arr[j].property < arr[j+1].property){
                    let temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    return (arr);
}

console.log(bubbleSort(data));


Solution

    • Since you're accessing the index j + 1, you should loop up to arr.length - 1.
    • Use bracket notation to access properties dynamically.

    const data = [
        {
             "color": "Blue",
             "door": 3,
             "wheel": 3,
             "year": 2005,
             "brand": "GMC",
             "sold": false,
             "owner": "Chalmers Boobyer",
             "motor": 1.7,
             "assembled": "09/08/2022"
         },
         {
             "color": "Indigo",
             "door": 4,
             "wheel": 4,
             "year": 1996,
             "brand": "Lincoln",
             "sold": true,
             "owner": "Morten Coffin",
             "motor": 1.7,
             "assembled": "26/08/2021"
         }
    ];
    
    
    function bubbleSort(arr, ord='asc', property){
        if(ord === 'asc'){
            for(let i = 0; i < arr.length; i++){
                for(let j = 0; j < arr.length - 1; j++){
                    if(arr[j][property] > arr[j+1][property]){
                        let temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
        } else if(ord === 'desc'){
            for(let i = 0; i < arr.length; i++){
                for(let j = 0; j < arr.length - 1; j++){
                    if(arr[j][property] < arr[j+1][property]){
                        let temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
        }
        return (arr);
    }
    
    console.log(bubbleSort(data, 'desc', 'door'));