4 Replies Latest reply on Oct 19, 2012 2:25 AM by rajvr5

    how to sort alphanumeric values in datagrid numerically

    rajvr5

      Hi all,

      I have a datagrid column which contains AlphaNumeric values.Is there a way i can sort these values in a numerical order

       

      eg:

       

      having d1,d11,12,13,d2,d3,d4

       

      d2 should come after d1..

       

      order should be d1,d2,d3,d4,d11,d12,d13

       

      Please help me out..if any have any idea..giving below thw code which is sorting as string

       

      protected function ColumnSortCompare( obj1:Object, obj2:Object ):int

          {

              if ( !obj1 && !obj2 )

                  return 0;

             

              if ( !obj1 )

                  return 1;

             

              if ( !obj2 )

                  return -1;

             

              var obj1Data:String = ComplexColumnData( obj1 ).toString();

              var obj2Data:String = ComplexColumnData( obj2 ).toString();

             

              if ( obj1Data < obj2Data )

                  return -1;

             

              if ( obj1Data > obj2Data )

                  return 1;

             

              return 0;

          }

       

       

      Thanx in advance

      Rajesh

        • 1. Re: how to sort alphanumeric values in datagrid numerically
          jfb00 Level 3

          Hi,

          Try this:

           

          import mx.utils.ObjectUtil;

           

          protected function ColumnSortCompare( obj1:Object, obj2:Object ):int{

                 return ObjectUtil.stringCompare(obj1[fieldName], obj2[fieldName]);

          }

          • 2. Re: how to sort alphanumeric values in datagrid numerically
            rajvr5 Level 1

            Hi,

             

            Thanks for the reply. Please dont mind if the question is simple,as I am new to flex.

             

            While I am using

             

            return ObjectUtil.stringCompare(obj1[fieldName], obj2[fieldName]);

             

            it is throwing error as, undefined property fieldName.

             

            I am calling as this

             

            <mx:DataGridColumn headerText=""

                                       editable="false"

                                       sortCompareFunction="ColumnSortCompare"

                                       textAlign="left"

                                       dataField="{PORT_NAME}"

                                       width="150"/>

             

            How I can resolve this.Please guide me.

             

            Thanks,

            Raj

            • 3. Re: how to sort alphanumeric values in datagrid numerically
              jfb00 Level 3

              Try this instead:

               

              protected function ColumnSortCompare(fieldName:String ):Function{

                   return function  ( obj1:Object, obj2:Object ):int

                  {

                     return ObjectUtil.stringCompare(obj1[fieldName], obj2[fieldName]);

                  }

              }

               

              Call the function as: sortCompareFunction="ColumnSortCompare('PORT_NAME')"

              Best,

              • 4. Re: how to sort alphanumeric values in datagrid numerically
                rajvr5 Level 1

                Hi,

                 

                Thanks for ur time.

                 

                But,Now its throwing error - Access of undefined property

                 

                ColumnSortCompare('PORT_NAME')

                 

                Any guidance will be very much helpful.

                 

                Thanks,

                Rajesh

                 

                Message was edited by: rajvr5