3 Replies Latest reply on Jan 26, 2007 11:33 AM by ur_dtrain

    Customized sorting in datagrid

    SebastianCarlsson
      I've just started to look at flex, could be a really simple answer to this... Newbie warning!

      Let's say that I want to sort a datagrid by a column called 'priority'. The values of 'priority' can be Low, Normal and High. Obviously I want rows with 'High' priority to be sorted first, then 'Normal' and then 'Low'. How do I do that?
        • 1. Re: Customized sorting in datagrid
          ur_dtrain Level 1
          create a sort function like this:
          package controls.sorters {
          // import flash.util.trace;
          public class Sorter {
          private var priorityLU:Object={Low:0,Normal:1,High:2};
          public function Sorter(){
          }
          public function sortPriority(obj1:Object, obj2:Object, col:Array = null):Number{
          if (priorityLU[obj1["priority"]] > priorityLU[obj2["priority"]]){
          return -1;
          }else if(priorityLU[obj2["priority"]] == priorityLU[obj1["priority"]] ){
          return 0;
          }else{
          return 1;
          }
          }
          }
          }

          this is then used like:
          <ns1:Sorter id="mySorter" />
          <mx:DataGridColumn id="priorityCol" width="100" headerText="Priority" dataField="priority" sortCompareFunction="mySorter.sortPriority"/>
          • 2. Re: Customized sorting in datagrid
            SebastianCarlsson Level 1
            Thank you very much, however, I get this error when I build my application:

            "The prefix "ns1" for element "ns1:Sorter" is not bound"

            Any ideas? Thx
            • 3. Re: Customized sorting in datagrid
              ur_dtrain Level 1
              ns1 is just the default name space for custom components - in your application tag add this:

              <mx:Application ........ xmlns:ns1="YourPathToTheSortCode.*">

              For example I have my Sort class in the folder: /controls/sorters/Sorter.as
              so I use: xmlns:sort="controls.sorters.*" and create the component as:
              <sort:Sorter />

              The ns1 is the default that Flex assigns if you drag your custom component on the stage from the component listing panel.