Sorting array against other array

Discussion in 'Javascript' started by Andrew Poulos, Jan 17, 2007.

  1. 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
     
    Andrew Poulos, Jan 17, 2007
    #1
    1. Advertisements

  2. Andrew Poulos

    Lee Guest

    Andrew Poulos said:
    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>


    --
     
    Lee, Jan 17, 2007
    #2
    1. Advertisements

  3. Andrew Poulos

    Matt Kruse Guest

    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.
     
    Matt Kruse, Jan 17, 2007
    #3
  4. In comp.lang.javascript message <[email protected]
    qv1-newsreader-01.iinet.net.au>, Wed, 17 Jan 2007 12:24:55, Andrew
    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.
     
    Dr J R Stockton, Jan 18, 2007
    #4

  5. 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
     
    Andrew Poulos, Jan 23, 2007
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.