Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

beginner here!

Recently stumbled upon a problem. Basically, the program needs to sort an array of objects by one of their fields without actually using the sort function. I've tried this code using bubble sort algorithm, but it doesn't seem to be working:

var arrayOfPeople = [
    {name: "Rick", age: 30, place: 2},
    {name: "Alan", age: 25, place: 1},
    {name: "Joe", age: 40, place: 4},
    {name: "Dave", age: 35, place: 3}
];


function bubbleSort(a,par)
{
    var swapped;

    do {
        swapped = false;

        for (var i = 0; i < a.length - 1; i++) {
            if (a[i].par > a[i + 1].par) {
                var temp = a[i];

                a[i] = a[i + 1];
                a[i + 1] = temp;

                swapped = true;
            }
        }
    } while (swapped);
}


bubbleSort(arrayOfPeople,'age');

for (i = 0; i < arrayOfPeople.length; i++) {
    console.log(arrayOfPeople[i]);
}

My guess is that I'm doing something wrong syntax-wise. Will appreciate any feedback.

share|improve this question
It might be that you haven't initialized you swapped-variable befor the do-while loop starts. Try replacing var swapped with var swapped = false – 7SLEVIN 15 mins ago
"without actually using the sort function" - why? – thg435 5 mins ago

1 Answer

up vote 3 down vote accepted

First of all, JavaScript has no "by reference" for function arguments. The array is changed only inside the function. To "solve" this you can have the function return the sorted array and use it when calling the function.

Second, you were not using the "par" argument correctly. The obj.prop syntax will always try to look for property named "prop" so to have it dynamic you need to use square brackets e.g. obj["prop"] which can get variable instead of "prop". Here is revised code that works:

function bubbleSort(a,par)
{
    var swapped;
    do {
        swapped = false;
        for (var i=0; i < a.length-1; i++) {
            if (a[i][par] > a[i+1][par]) {
                var temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                swapped = true;
            }
        }
    } while (swapped);
    return a;
}


var sortedArray = bubbleSort(arrayOfPeople, 'age');

for (i = 0; i < sortedArray.length; i++) {
   console.log(sortedArray[i]);
}

Live test case.

You can also overwrite the original array if you prefer:

arrayOfPeople = bubbleSort(arrayOfPeople, 'age');
share|improve this answer
Can you clarify your first statement (JavaScript has no "by reference" for function arguments)? a is definitely passed into bubbleSort by reference, or do you imagine that it is copied, and a copy thereof is passed into bubbleSort instead? Sorting the array in-place inside bubbleSort will definitely work. – Alexander Pavlov 1 min ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.