A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Array sort/ size reduction

  1. #1
    Junior Member scudsucker's Avatar
    Join Date
    Feb 2003
    Location
    Cape Town, RSA
    Posts
    1,509

    Array sort/ size reduction

    Hi- in making an XML text "search engine" I've run into a problem which I cannot solve. No matter how much I puzzle over it I am stuck in the same rut. I really need a good hard kick to get me out.

    Let me simplify:

    With
    code:

    var aTemp=new Array();
    aTemp[0]="A";
    aTemp[1]="B";
    aTemp[2]="C";
    aTemp[3]="A";
    aTemp[4]="A";



    I would like to get from this another array, with values
    code:

    aFinal[0]="A"
    aFinal[1]="B"
    aFinal[2]="C"



    I cannot work out how to do it though... Thanks for any hints
    Hariyemadzisawira nhaka yedu! Down the SCUD and win!
    I'm too lazy to read Private Messages.

  2. #2
    Procrastinator
    Join Date
    Oct 2000
    Location
    Osaka
    Posts
    138
    hello.
    I'm not totally sure what you're getting at, but if it's that you want to eliminate repetition in the final array, i think this should do it:

    code:

    var aTemp = new Array();
    aTemp[0] = "A";
    aTemp[1] = "B";
    aTemp[2] = "C";
    aTemp[3] = "A";
    aTemp[4] = "A";
    aTemp[5] = "A";
    aTemp[6] = "C";
    var aFinal = new Array();
    aFinal.push("x");// Just to get it going. There's probably a better way to do it.
    function loopTemp()
    {
    for (var i in aTemp)
    {
    elemTemp = aTemp[i];
    loopFinal(elemTemp);
    }
    }
    function loopFinal(element)
    {
    for (var j in aFinal)
    {
    if (aFinal[j] == element)
    {
    return;
    }
    }
    aFinal.push(element);
    return;
    }
    loopTemp();
    aFinal.splice(0,1);// Remove that 'x'!
    aFinal.sort();// Not really necessary.



    cheers,
    pog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center