sorting an associative array keys based on values

Discussion in 'Javascript' started by soup_or_power@yahoo.com, Jun 20, 2005.

  1. Guest

    Hi
    I have an associative array like this:
    arr[x1]=30; arr[x2]=20;arr[x3]=40;arr[x4]=10;

    I want the sort function to sort keys in ascending order of the values
    on the right hand side with the following result:
    x4,x2,x1,x3

    Can anyone please help me write the function?
    Thank you
     
    , Jun 20, 2005
    #1
    1. Advertising

  2. caston Guest

    wrote:
    > Hi
    > I have an associative array like this:
    > arr[x1]=30; arr[x2]=20;arr[x3]=40;arr[x4]=10;
    >
    > I want the sort function to sort keys in ascending order of the values
    > on the right hand side with the following result:
    > x4,x2,x1,x3
    >
    > Can anyone please help me write the function?
    > Thank you


    <script language="JavaScript">
    // create an array
    var arr = [];
    arr["x1"]=30;
    arr["x2"]=20;
    arr["x3"]=40;
    arr["x4"]=10;

    // show the current array
    for (var sKey in arr)
    document.write(sKey + ':' + arr[sKey] + '; ');
    document.write('<br />');

    // sort 'array'
    var arr2 = sortAssoc(arr);

    // show the sorted array
    for (var sKey in arr2)
    document.write(sKey + ':' + arr2[sKey] + '; ');
    document.write('<br />');

    // And here comes the funciton itself
    function sortAssoc(aInput)
    {
    var aTemp = [];
    for (var sKey in aInput)
    aTemp.push([sKey, aInput[sKey]]);
    aTemp.sort(function () {return arguments[0][1] < arguments[1][1]});

    var aOutput = [];
    for (var nIndex = aTemp.length-1; nIndex >=0; nIndex--)
    aOutput[aTemp[nIndex][0]] = aTemp[nIndex][1];

    return aOutput;
    }
    </script>

    Hope I helped you/
    Sergey.
     
    caston, Jun 20, 2005
    #2
    1. Advertising

  3. Csaba Gabor Guest

    wrote:
    > Hi
    > I have an associative array like this:
    > arr[x1]=30; arr[x2]=20;arr[x3]=40;arr[x4]=10;
    >
    > I want the sort function to sort keys in ascending order of the values
    > on the right hand side with the following result:
    > x4,x2,x1,x3


    If you can guarantee unique numeric keys on the right...

    function assocSort (oAssoc) {
    var idx; var key; var arVal = []; var arValKey = []; var oRes = {};
    for (key in oAssoc) {
    arVal[arVal.length] = oAssoc[key];
    arValKey[oAssoc[key]] = key;
    }
    arVal.sort();
    for (idx in arVal)
    oRes[arValKey[arVal[idx]]] = arVal[idx];
    return oRes;
    }

    var arr = {x1:30, x2:20, x3:40, x4:10}
    var arrSorted = assocSort(arr);


    Csaba Gabor from Vienna
     
    Csaba Gabor, Jun 20, 2005
    #3
  4. VK Guest

    > I have an associative array
    Thank you for your perfect language
    ;-) :-|

    > I want the sort function to sort keys
    > in ascending order of the values


    (1) First of all, you may want to use the standard associative array
    syntacs for predefined values (given that x1...x4 are real variables in
    your script)

    var map = { x1 : 30, x2 : 20 , x3 : 40, x4 : 10};

    (2) JavaScript doesn't have an associative array as a programming
    entity. So any more or less complicated operations over an associative
    array have to be programmed manually by yourselve. In your case there
    is no way (?) to accomplish that w/o multiple pass over the hash
    table.
    To be continued... (if you still need it)
     
    VK, Jun 20, 2005
    #4
  5. writes:

    > I have an associative array like this:
    > arr[x1]=30; arr[x2]=20;arr[x3]=40;arr[x4]=10;


    I guess you have
    var arr = new Object();
    or something, as well as the variables x1..x4 declared.

    > I want the sort function to sort keys in ascending order of the values
    > on the right hand side with the following result:
    > x4,x2,x1,x3


    I assume values are always numbers. Otherwise a less trivial
    comparison function is needed.

    function sortByValue(keyArray, valueMap) {
    return keyArray.sort(function(a,b){return valueMap[a]-valueMap;});
    }

    testing:

    var arr = {foo: 30, bar: 20, baz:40, doh: 10};
    var keyArray = ["foo","bar","baz","doh"]
    alert(sortByValue(keyArray, arr));

    > Can anyone please help me write the function?


    There you go.
    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jun 20, 2005
    #5
  6. Randy Webb Guest

    Lasse Reichstein Nielsen wrote:

    > writes:
    >
    >
    >>I have an associative array like this:
    >>arr[x1]=30; arr[x2]=20;arr[x3]=40;arr[x4]=10;

    >
    >
    > I guess you have
    > var arr = new Object();
    > or something, as well as the variables x1..x4 declared.


    I may have read it wrong but I read it as more like arr['x1'] where the
    quotes were forgotten. If the sort is to be done by strings (if they are
    indeed strings) then its a simple matter of .sort() and .reverse().

    If the sort is based on the value of the variables, then you wouldn't
    always get the order x4, x3, x2, x1 unless you grab the variable name
    itself and create your own list and then sort and reverse it.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
     
    Randy Webb, Jun 21, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Fredstone
    Replies:
    1
    Views:
    411
    =?ISO-8859-1?Q?Tobias_Schr=F6er?=
    Sep 19, 2006
  2. desktop
    Replies:
    5
    Views:
    396
    James Kanze
    Jun 26, 2007
  3. Replies:
    20
    Views:
    807
    Barry
    Sep 12, 2007
  4. Nathan Olson

    Sorting an associative array

    Nathan Olson, Jun 27, 2004, in forum: Perl Misc
    Replies:
    27
    Views:
    522
    Kevin Collins
    Jun 29, 2004
  5. laredotornado

    How do I get keys from an associative array?

    laredotornado, Feb 21, 2010, in forum: Javascript
    Replies:
    5
    Views:
    118
    Dr J R Stockton
    Feb 25, 2010
Loading...

Share This Page