Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading