4

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.

Darkzaelus
  • 2,059
  • 1
  • 15
  • 31
nainy
  • 520
  • 1
  • 4
  • 21
  • 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` – mfaerevaag Jun 06 '13 at 08:00
  • Use native `sort` will be faster http://stackoverflow.com/a/1129270/1346222 – b1_ Jun 06 '13 at 09:15

2 Answers2

7

The only problem was that 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".

You didn't get any errors as a[i].par and a[i+1].par both returned undefined which can be compared to itself. (hence a[i].par > a[i+1].par always returns false)

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);
}


bubbleSort(arrayOfPeople, 'age');

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

Live test case.

Worth to mention in this context, that the function changing the actual object (array in this case) is not a trivial thing. To learn more what is passed by value and what is passed by reference take a look in this excellent question: Is JavaScript a pass-by-reference or pass-by-value language?

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • 2
    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 Jun 06 '13 at 08:14
  • @AlexanderPavlov blonde moment/ignorance on my part you're totally correct. I have revised my answer. Thanks! :) – Shadow The GPT Wizard Jun 06 '13 at 08:46
  • np. Great answer otherwise! – Alexander Pavlov Jun 06 '13 at 09:11
4

Use the built in array sort function:

arrayOfPeople.sort(function(a,b) {return a.age-b.age;});
TheSavage
  • 269
  • 1
  • 10