473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Sorting array against other array

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
Jan 17 '07 #1
4 3471
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[i].info,"<br>");
}
</script>
</body>
</html>
--

Jan 17 '07 #2
Andrew Poulos wrote:
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[i] = [this[i]];
for (var j=0; j<arguments.length; j++) {
if (arguments[j].length!=this.length) { return false; }
sortArray[i][j+1] = arguments[j][i];
}
}
sortArray.sort();
for (var i=0; i<sortArray.length; i++) {
this[i] = sortArray[i][0];
for (var j=0; j<arguments.length; j++) {
arguments[j][i] = sortArray[i][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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jan 17 '07 #3
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 <ap*****@hotmail.composted:
>
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 18 '07 #4
Matt Kruse wrote:
Andrew Poulos wrote:
>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[i] = [this[i]];
for (var j=0; j<arguments.length; j++) {
if (arguments[j].length!=this.length) { return false; }
sortArray[i][j+1] = arguments[j][i];
}
}
sortArray.sort();
for (var i=0; i<sortArray.length; i++) {
this[i] = sortArray[i][0];
for (var j=0; j<arguments.length; j++) {
arguments[j][i] = sortArray[i][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
Jan 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
25
by: Allie | last post by:
How would I go about sorting this structure by title? typedef struct { char author; char title; char code; int hold; int loan; } LIBRARY;
11
by: Registered User | last post by:
What is the name of the following sorting method? for (i=0; i<n-1; i++) for (j=i+1; j<n; j++) if (a>a) swap(&a, &a); It seems to be the opposite of bubble sort, with lighter elements going...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
1
by: arnuld | last post by:
On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote: An abstract overview of my own program by Ben had helped me focus on the design issue first. I came to know that whole problem went...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.