A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: array sort by value - easy?

  1. #1
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472

    array sort by value - easy?

    hi there,

    I pulling data from a DB split it into to array and now I would like to sort it depending on a specific value.

    myArr[0] = ["nameA",0,"mail"]
    myArr[1] = ["nameB",2,"mail"]
    myArr[2] = ["nameC",1,"mail"]
    myArr[3] = ["nameD1",1,"mail"]
    myArr[4] = ["nameE",0,"mail"]
    myArr[5] = ["nameF",2,"mail"]

    I would like to sort the array depending on the second value.
    All values of '1' should go first. Followed by all values of '0' and then all values of '2'.

    To make it look like this
    myArr[0] = ["nameC",1,"mail"]
    myArr[1] = ["nameD",1,"mail"]
    myArr[2] = ["nameA",0,"mail"]
    myArr[3] = ["nameE",0,"mail"]
    myArr[4] = ["nameB",2,"mail"]
    myArr[5] = ["nameF",2,"mail"]

    Am I making myself understandable?

    Regards
    //PODEN
    Last edited by podenphant; 08-13-2003 at 07:31 AM.

  2. #2
    Member
    Join Date
    Aug 2003
    Posts
    37
    Looks like it's possible with sortOn(), but you'd have to enter the array elements as objects instead of arrays.

    From the help files on sortOn() method of arrays:

    This following example creates a new array and sorts it based on the field city:

    var recArray = new Array();
    recArray.push( { name: "bob", city: "omaha", zip: 68144 } );
    recArray.push( { name: "greg", city: "kansas city", zip: 72345 } );
    recArray.push( { name: "chris", city: "burlingame", zip: 94010 } );
    recArray.sortOn("city");
    // results in the following:
    recArray[0] = name: "chris", city: "burlingame", zip: 94010
    recArray[1] = name: "greg", city: "kansas city", zip: 72345
    recArray[2] = name: "bob", city: "omaha", zip: 68144


    Maybe this helps?
    Pauline

  3. #3
    Senior Member
    Join Date
    Mar 2003
    Location
    Ottawa, Canada
    Posts
    104
    Try this....

    myArr = new Array();
    myArr[0] = ["nameA",0,"mail"];
    myArr[1] = ["nameB",2,"mail"];
    myArr[2] = ["nameC",1,"mail"];
    myArr[3] = ["nameD1",1,"mail"];
    myArr[4] = ["nameE",0,"mail"];
    myArr[5] = ["nameF",2,"mail"];

    // Storing the order in array (first 1, then 0, then 2)
    var theOrder = new Array(1, 0, 2);
    // Storing an increment value for use with theOrder
    var whichElement = 0;

    // Declaring array that store newly sorted array values.
    var newMyArr = new Array();
    // Increment for use with newMyArr
    var elementIncrement = 0;

    // Looping though all patterns from theOrder
    for (var j=0;j<=theOrder.length;j++) {

    // Looping though all the elements of myArr
    for (var i=0;i<myArr.length;i++) {

    // If the 1 element of myArr[i] matches the first pattern(theOrder[0])
    // Add that elements elements to the new array (newMyArr)
    // then increment elementIncrement ready to write newMyArr
    //next element
    if (myArr[i][1]==theOrder[whichElement]) {
    newMyArr[elementIncrement] = [myArr[i][0], myArr[i][1], myArr[i][2]];
    elementIncrement++;
    }
    }

    // If there is no match, increment whichElement, (to start looking //for the next pattern (theOrder[1]), etc)
    whichElement++;
    }
    // Re-assign the value of myArr with newly contructed array newMyVar

    myArr = newMyArr;
    // Cleaning up temp variables.....
    delete newMyArr;
    delete whichElement;
    delete theOrder;
    MX User.
    ------------------
    http://www.michaeldilts.com

  4. #4
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472
    Nicely done mykal - works great!

    thanks
    K-pod

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