3 Replies Latest reply on May 7, 2008 12:36 PM by ntsiii

    How do I sort children in a vbox?

      If I have a vbox being populated by children, is there any way to sort those children? I'll try to describe my example.

      VBox A is supplied with 4 types of children, aC bC cC dC (name is attached to the child object). I would like to make it so any child named aC will always appear on the top, bC next, cC after that, and dC on the bottom. I can't seem to find any reference to this everywhere I've looked, it seems like it'd be a pretty common procedure used to maintain design fidelity.
      Any help would be appreciated, I'd be glad to post my code if anyone thinks they might have an idea.
        • 1. Re: How do I sort children in a vbox?

          To sort the children, you have to access the children, test each, and sort accordingly (depending upon what type of sort-method you want to use -- bubble-sort, merge-sort, etc.)...

          You'll have to test the name of each child component (assuming that 'name' is the data-field you want to sort on), and position/reorder/swap (ie -- sort), based upon the ordering ('alphabetical'?) comparison criteria.

          The code might look something like the (hopefully) attached code...

          Note that areOutOfOrder() would be some sort of test...:

          if child.name > nextChild.name return FALSE, else return TRUE

          You can implement this one of 2 ways: internal to a subclass of VBox (such as class MyVBoxClass), or external to it, such as within the application.

          If you want to assure the children remain sorted, that is to sort during dynamic runtime adding/removing children, you'll have to listing to the vBox added/removed events, and call the sorting routine to make sure the children remain sorted...

          myVBox.addEventListener(flash.events.ADDED, myAddedEventHandlerFunction);
          myVBox.addEventListener(flash.events.REMOVED, myRemovedEventHandlerFunction);

          Note that on removal, assuming the items were already sorted, they PROBABLY won't need to be resorted.

          Hope that helps.
          • 2. Re: How do I sort children in a vbox?
            Gregory Lafrance Level 6
            I wonder if you could store information on the children (name or whatever you sort on) in an ArrayCollection, and then sort that using a sortFunction. Then if the ArrayCollection gets triggered for a resort, remove and readd the children based on their order in the AC.
            • 3. Re: How do I sort children in a vbox?
              ntsiii Level 3
              Are you adding these children manually, using mxml tags? If so, then sort doesn't make much sense.

              If they are being added dynamically, consider using Repeater for instantiation/removal, and then just sort the dataProvider.