dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: sortItemsBy not working

  1. #1
    Senior Member
    Join Date
    Jan 2001
    Posts
    105

    sortItemsBy not working

    I'm having problems using the sortItemsBy command.

    I'm populating the Listbox using the distance formula based on button locations on the Stage:
    distance = Math.sqrt(Math.pow((currentX-Math.floor(getProperty(_root.selectedButton, _x))), 2)+Math.pow((currentY-Math.floor(getProperty(_root.selectedButton, _y))), 2));
    distanceListbox.addItem(distance, eval(buttonsBelow.getItemAt(k).label));

    I'm then trying to sort the listbox based on the values returned:
    distanceListbox.sortItemsBy("label", "ASC");

    I can see the values, but they don't appear to be sorting properly all of the time.

    Anyone have a clue why this might be occuring?
    Mario Cascio
    Webpromotion, Inc.
    http://www.webpromotion.com

    Royalty Free 3D Flash Animation
    http://www.webpromotion.com/fp01.html

  2. #2
    Senior Member
    Join Date
    Jan 2001
    Posts
    105
    ^^ bump ^^

    Anyone have any ideas?
    Mario Cascio
    Webpromotion, Inc.
    http://www.webpromotion.com

    Royalty Free 3D Flash Animation
    http://www.webpromotion.com/fp01.html

  3. #3
    Senior Member
    Join Date
    Jan 2001
    Posts
    105
    OK,

    In my efforts to resolve this issue, I've done some simple tests and am very confused.

    I've created array that I simply want to sort:
    filterDistanceArray.push(241.505693514666);
    filterDistanceArray.push(187.096231923575);
    filterDistanceArray.push(289.830985231048);
    filterDistanceArray.push(78.1024967590665);
    filterDistanceArray.sort();
    for (i=0; i<filterDistanceArray.length; i++) {
    trace("sortedfilterDistanceArray ="+filterDistanceArray[i]);
    }

    The Output window reports:
    sortedfilterDistanceArray =187.096231923575
    sortedfilterDistanceArray =241.505693514666
    sortedfilterDistanceArray =289.830985231048
    sortedfilterDistanceArray =78.1024967590665

    Any reason why 78.102 is at the bottom of the list?
    Mario Cascio
    Webpromotion, Inc.
    http://www.webpromotion.com

    Royalty Free 3D Flash Animation
    http://www.webpromotion.com/fp01.html

  4. #4
    Developer
    Join Date
    Sep 2001
    Location
    The Bluegrass State Will Flash For Food
    Posts
    3,789
    Originally posted by mcascio
    Any reason why 78.102 is at the bottom of the list?
    sort() sorts by alpha not numeric. Thats why 7 comes after 1.

    here you go:
    Code:
    numeric = function (a, b) {
    	return (a > b);
    };
    filterDistanceArray = new Array();
    filterDistanceArray.push(241.505693514666);
    filterDistanceArray.push(187.096231923575);
    filterDistanceArray.push(289.830985231048);
    filterDistanceArray.push(78.1024967590665);
    filterDistanceArray.sort(numeric);
    for (i = 0; i < filterDistanceArray.length; i++) {
    	trace("sortedfilterDistanceArray = " + filterDistanceArray[i]);
    }

  5. #5
    Senior Member
    Join Date
    Jan 2001
    Posts
    105
    Thanks gSOLO_01.

    That did it.

    Now another question that digs deeper.

    I'm trying to store two values in an array.
    The distance along with the object name which might look like:
    74, object1
    120, object2
    430, object3
    12, object4

    Then I need to sort by the distance but then return the actual object name based on the smallest distance.

    I believe I may need to use a multidimensional array, but I'm not sure.

    Thanks again for any help.
    Mario Cascio
    Webpromotion, Inc.
    http://www.webpromotion.com

    Royalty Free 3D Flash Animation
    http://www.webpromotion.com/fp01.html

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