Sorting array against other array

A

Andrew Poulos

Say I have two arrays with the same number of elements that are related:

var a1 = [1,3,2];
var b2 = ["a","b","c"];

I need to sort a1 but have the order of the elements in b2 reflect the
any new order in a1. So if a1 sorts as

[1,2,3] then b2 becomes
["a","c","b"]

or if a1 sorts as

[3,2,1] then b2 becomes
["b","c","a"]

Andrew Poulos
 
L

Lee

Andrew Poulos said:
Say I have two arrays with the same number of elements that are related:

var a1 = [1,3,2];
var b2 = ["a","b","c"];

I need to sort a1 but have the order of the elements in b2 reflect the
any new order in a1. So if a1 sorts as

[1,2,3] then b2 becomes
["a","c","b"]

or if a1 sorts as

[3,2,1] then b2 becomes
["b","c","a"]

If possible, combine your two arrays into an array of objects that each have two
fields, then define a custom sort function that compares only the fields that
you want to sort by. This example assumes that the sort key is numeric:

<html>
<body>
<script type="text/javascript">

var pair = [ {key:1, info:"a"},
{key:3, info:"b"},
{key:2, info:"c"}
];

function comparePair(a,b) {
return a.key-b.key;
}

pair.sort(comparePair);
for (var i=0;i<pair.length;i++) {
document.write(pair.info,"<br>");
}
</script>
</body>
</html>


--
 
M

Matt Kruse

Andrew said:
Say I have two arrays with the same number of elements that are
related: var a1 = [1,3,2];
var b2 = ["a","b","c"];
I need to sort a1 but have the order of the elements in b2 reflect the
any new order in a1. So if a1 sorts as

I haven't done extensive testing, but I threw this together:

var a1 = [1,3,2,5,4];
var a2 = ["a","b","c","d","e"];
var a3 = ["z","y","x","w","v"];

Array.prototype.multisort = function() {
var sortArray = [];
for (var i=0; i<this.length; i++) {
sortArray = [this];
for (var j=0; j<arguments.length; j++) {
if (arguments[j].length!=this.length) { return false; }
sortArray[j+1] = arguments[j];
}
}
sortArray.sort();
for (var i=0; i<sortArray.length; i++) {
this = sortArray[0];
for (var j=0; j<arguments.length; j++) {
arguments[j] = sortArray[j+1];
}
}
return true;
}

alert(a1+"\n"+a2+"\n"+a3);
a1.multisort(a2,a3);
alert(a1+"\n"+a2+"\n"+a3);
a2.multisort(a1,a3);
alert(a1+"\n"+a2+"\n"+a3);
a3.multisort(a1,a2);
alert(a1+"\n"+a2+"\n"+a3);


Let me know if it works for you.
 
D

Dr J R Stockton

In comp.lang.javascript message <45ad7ae6$0$27931$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Wed, 17 Jan 2007 12:24:55, Andrew
Poulos said:
I need to sort a1 but have the order of the elements in b2 reflect the
any new order in a1.

You could replace the content of each element of A1 with an Object
containing the previous content and also the corresponding element of
B1; then sort; then regenerate B1.

If you have lots of subsidiaries, say b00 to b99, you could extend that.
Or you could add as above to each element of A1 its index; sort; extract
the sorted indexes and use them to rearrange every bxx. I doubt
whether, in Javascript, that would be better.

If you have editable sorting code, you can just modify it so where it
swaps a1 it also swaps b2. But that should be less efficient.
 
A

Andrew Poulos

Matt said:
Andrew said:
Say I have two arrays with the same number of elements that are
related: var a1 = [1,3,2];
var b2 = ["a","b","c"];
I need to sort a1 but have the order of the elements in b2 reflect the
any new order in a1. So if a1 sorts as

I haven't done extensive testing, but I threw this together:

var a1 = [1,3,2,5,4];
var a2 = ["a","b","c","d","e"];
var a3 = ["z","y","x","w","v"];

Array.prototype.multisort = function() {
var sortArray = [];
for (var i=0; i<this.length; i++) {
sortArray = [this];
for (var j=0; j<arguments.length; j++) {
if (arguments[j].length!=this.length) { return false; }
sortArray[j+1] = arguments[j];
}
}
sortArray.sort();
for (var i=0; i<sortArray.length; i++) {
this = sortArray[0];
for (var j=0; j<arguments.length; j++) {
arguments[j] = sortArray[j+1];
}
}
return true;
}

alert(a1+"\n"+a2+"\n"+a3);
a1.multisort(a2,a3);
alert(a1+"\n"+a2+"\n"+a3);
a2.multisort(a1,a3);
alert(a1+"\n"+a2+"\n"+a3);
a3.multisort(a1,a2);
alert(a1+"\n"+a2+"\n"+a3);


Let me know if it works for you.

Sorry, my example used numerals but the stuff I'm working on also uses
strings.

Thanks, what you did works fine - it correctly puts "Test" before
"apple". Except I need it to sort, where strings, alphabetically and not
based on ASCII value. The case of the elements of the arrays is not
supposed to change so should I copy the "base" array; convert each
element's case to lower case, and then do the sort using it?

Andrew Poulos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top