Adding FLEXibility to sorting hierarchical data

With the release of Flex 3 Moxie Beta 2, sorting of hierarchical data has been given a new FLEXibility.

Earlier sorting hierarchical data was not possible. With the introduction of Advanced DataGrid and new collections support, we can have new ways to sort Hierarchical data.

Check out the sample here

1. In the first Grid – Sorting can be done recursively on all the nodes and their children.

2. In the second Grid a node can be selected and only its children can be sorted and sort order can be chosen from the ComboBox.

Source code can be found here


9 thoughts on “Adding FLEXibility to sorting hierarchical data

  1. Hi, Iam using groupCollection as data provider in my advance dataGrid.I have my Summary row placed at Group level which in turn is a summary of computed column. i.e I have a field which is showing financial data with a $ sign(Computed Column) and the same field is my summary field.

    Now I want to sort my data based on summary Row.I tried using default sorting but it doesnt work and when I sort group column it sorts only child of all nodes not parent nodes.
    How should i go about it.Please help its urgent…

  2. For sorting Group Columns make sure that you make the label property in Grouping same as the column dataField.
    For example, if the column’s dataField is “name”.
    Create your Grouping as –

    var gf:Grouping = new Grouping();
    gf.label = "name";
    gf.fields = [new GroupingField("name")];

    For summaries, you can write a custom sort function to do that.

  3. Hi,
    When I run your exmple from this page, it works perfectly fine , but when I copy and paste the mxml file to my flex project and run it from there, the right depthwise sorting doesn’t work at all! I know this is so weired! do you have any idea why? I’m using flex 3.0


    • This code is compiled with Flex 3 Beta 2 as written in the post. So, things might have changed.
      Basically, what’s happening here is the sort on the child collection is overwritten by the sort on the IHierarchicalCollection (dataProvider) which is null (there is no sort on the collection).

  4. Hi again,
    I thought maybe you can help me since you worked in this area.
    I need to do something similar to this! I have advancedDataGrid with flatData. In my UI, users can add grouping and summaries, and filters to ADG. for sorting I want to be able to sort using all the columns no matter if the column is grouped or not!
    let’s say the columns are Country, Province, City and Population. and the user grouped the data based on Country and Province.
    so sorting based on country, will sort the group of countries, and soring on Province will sort the group of Provinces and sort on city will do the sort all the cities under Province groups

    any help will be greatly appreciated,


    • In the scenario that you mentioned about Country, Province, City and Population with grouping on Country and Province, I think clicking on the City and Population header will be working fine and sorting the collection based on these fields.
      Sorting on the grouped fields: Country and Province might not be working. For sorting to work properly on the grouped fields, you need to set Grouping.label property to the dataField of the column in which you are showing it.
      Something like – if you are column’s dataField is Country –
      <mx:Grouping label=”Country”>
      <mx:GroupingField name=”Country” />

  5. Hi Sameer,

    I have a question on Sorting. I am using groupingcollection to group the flat data. I have a lablefunction on a column. Can you help me find a way to sort this column.

  6. Hi Sameer,

    I’ve two grouping option, Category and Status. We have some unique number along with the categories like, “302 Explosives”. Earlier after applying grouping on “category” field, column sorting was lost. To fix this I’ve applied “compareFunction”. Now, Sorting is working fine when we use group by on “category” field ( image -1) but when try with “Status” , it’s failing somewhere (image-2). Can anybody point my what’s wrong ?

    You can view the image here ,

    Here is the code,

    var mygroup:GroupingCollection=new GroupingCollection();

    mygroup.source = dominoData;

    var gf:GroupingField = new GroupingField(selItem);

    gf.compareFunction = DoSortItems;

    var group:Grouping = new Grouping();

    group.fields = [gf];

    mygroup.grouping = group;


    dataGrid.dataProvider = mygroup;

    and Comparefunction ,

    private function DoSortItems(a:Object, b:Object,fields:Array=null):int { return ObjectUtil.stringCompare(a.hdnum.toString(),b.hdnum.toString()); }


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s