Skip to main content
frameexpert
Community Expert
Community Expert
September 7, 2012
Answered

Sort algorithm: how can I sort two arrays according to one

  • September 7, 2012
  • 4 replies
  • 12786 views

I have two parallel arrays, similar to this:

var array1 = [4, 3, 1, 5, 2];

var array2 = ["four", "three", "one", "five", "two"];

I want to sort the first array, and have the second array follow the same sort as the first. Any advice for a good algorithm would be appreciated.

Rick Quatro

This topic has been closed for replies.
Correct answer bduffy323

give this a try!

var array1 = [4, 3, 1, 5, 2];

var array2 = ["four", "three", "one", "five", "two"];

bubbleSort(array1,array2);

function bubbleSort(a,b)

{

    var swapped;

    do {

        swapped = false;

        for (var i=0; i < a.length-1; i++) {

            if (a > a[i+1])

            {

                var temp = a;

                a = a[i+1];

                a[i+1] = temp;

                var temp = b;

                b = b[i+1];

                b[i+1] = temp;

                swapped = true;

            }

        }

    } while (swapped);

}

alert(array1);

alert(array2);

4 replies

Inspiring
September 7, 2012

var arr1 = ["9","3","4"];

var arr2 = ["nine","smile","clown"];

var arr = [];

function contentsIntoObjects(){

for(var i = 0; i<contents.length; i++){

    arr.push({

        arr1: arr1,

        arr2: arr2,

        });

    };

}; //contentsIntoObjects

contentsIntoObjects();

arr.sort(function(a, b){return a.arr1 - b.arr1});

frameexpert
Community Expert
Community Expert
September 7, 2012

Thanks for the excellent help. I like this one because it is pretty simple, and it is convenient to store an array of objects.

var docs = [];

docs.push({number:4,name:"four"});

docs.push({number:3,name:"three"});

docs.push({number:1,name:"one"});

docs.push({number:5,name:"five"});

docs.push({number:2,name:"two"});

alert (docs[0].name);

docs.sort(function(a, b) { return a.number - b.number; });

alert (docs[0].name);

One the other had, two arrays are convenient because I need one to populate a dropdownlist. So I might use bduffy's bubble sort.

BTW, here is what I am doing. I need to collect a list of open documents for use in a script. I am storing the file names without the path on one array and the document objects in another parallel array. I want to sort the file names and display them in a dropdownlist. When the user picks out a file name from the dropdownlist, I want to get the corresponding document object from the other array.

Thanks again for the generous help.

Rick

www.frameexpert.com
frameexpert
Community Expert
Community Expert
September 7, 2012

Since arrays get passed to functions by reference in JavaScript, this one works well as a general function for sorting parallel arrays. It preserves both arrays.

var numbers = [4, 3, 1, 5, 2];

var names = ["four", "three", "one", "five", "two"];

alert (numbers);

alert (names);

 

parallelArraySort (numbers, names);

 

alert (numbers);

alert (names);

 

function parallelArraySort (a1, a2) {

   

    // Make a temporary array of objects and populate it from the two arrays.

    var arr = [];

    for (var i = 0; i < a1.length; i += 1) {

        arr.push ({first: a1, second: a2});

    }

 

    // Sort the object based on the first array.

    arr.sort (function (a, b) { return a.first - b.first; });

 

    // Update both arrays, based on the new sort.

    for (var i = 0; i < arr.length; i += 1) {

        a1 = arr.first;

        a2 = arr.second;

    }

}

www.frameexpert.com
Inspiring
September 7, 2012

I had a similar issue earlier this week. I found this invaluable.

http://jsfiddle.net/7rygA/

Jongware
Community Expert
Community Expert
September 7, 2012

Give this a try!

var array1 = [4, 3, 1, 5, 2];
var array2 = ["four", "three", "one", "five", "two"];

var array3 = [];
for (i=0; i<Math.min(array1.length, array2.length); i++)
array3 = [ array1, array2 ];

array3.sort (function(a,b) { return a[0] - b[0]; });

alert (array3.join('\r'));

Harbs.
Legend
September 7, 2012

I would do it similar to Jongware's approach, but I'd probably make an array of objects instead of a two dimentional array.

Jongware
Community Expert
Community Expert
September 7, 2012

.. I wonder why this does not work .. Perhaps my logic is flawed?

var array1 = [4, 3, 1, 5, 2];
var array2 = ["four", "three", "one", "five", "two"];

var array3 = [];
for (i=0; i<Math.min(array1.length, array2.length); i++)
array3 = [ i ];

array3.sort (function(a,b) {
var swap, diff = array1 - array1;
if (diff < 0)
{
  swap = array1
;
  array1
= array1;
  array1 = swap;
  swap = array2
;
  array2
= array2;
  array2 = swap;
}
return diff;
});

alert (array1.join(' ')+'\r'+array2.join(' '));

bduffy323
bduffy323Correct answer
Inspiring
September 7, 2012

give this a try!

var array1 = [4, 3, 1, 5, 2];

var array2 = ["four", "three", "one", "five", "two"];

bubbleSort(array1,array2);

function bubbleSort(a,b)

{

    var swapped;

    do {

        swapped = false;

        for (var i=0; i < a.length-1; i++) {

            if (a > a[i+1])

            {

                var temp = a;

                a = a[i+1];

                a[i+1] = temp;

                var temp = b;

                b = b[i+1];

                b[i+1] = temp;

                swapped = true;

            }

        }

    } while (swapped);

}

alert(array1);

alert(array2);