9 Replies Latest reply on Mar 5, 2010 3:45 AM by Sebastien V.

    Sort by Date(String) in dataGrid

    zakabalt Level 1

      Hello, I want to sort by date my dataGrid but I have a problem. The Date is not a Date, it's a String and I don't know if it's for that but the dataGrid doesn't sort correctly. The Date(String) format is "dd/mm/yyyy hh:mm:ss" and I want to see in the top the last Date.

       

       

      I have to change the String to Date or exist another way to do that? If I have to change, which is the best way to do that?
        • 1. Re: Sort by Date(String) in dataGrid
          Sebastien V. Level 3

          You can use a Date as the dataField in the DataGridColumn, and use a labelFunction to format your Date as you wish for display.

          This way the sort will be made on the Date object, and the Date will be displayed as you want it to be.

           

          Plus changing the Date display format will be easy and won't change the sorting logic.

          • 2. Re: Sort by Date(String) in dataGrid
            Giuseppe Muccioli

            Hi zacabalt,

            if you don't want to change the date string format (correct is yyyy/mm/dd, hh:mm:ss) you have to define the sortCompareFunction of the DataGridColumn element.

            Something like:

             

            <mx:DataGridColumn dataField="theDateField" sortCompareFunction="myDateSort"/>

             

            where

             

            private function myDateSort(obj1:Object, obj2:Object):int {

                 //create the Date object from your string

                 var date1:Date=DateField.stringToDate(obj1.theDateField, "dd/mm/yyyy");

                 var date2:Date=DateField.stringToDate(obj2.theDateField, "dd/mm/yyyy");

                 if (date1<date2)

                      return -1;

                 if (date1>date2)

                           return 1;

                 return 0;

            }

             

            See reference manual for details.

            Hope I help you.

            Beppino

            • 3. Re: Sort by Date(String) in dataGrid
              zakabalt Level 1

              Your example is correct but I need also the hh/mm/ss and the method stringToDate only knows YYYY/MM/DD.

               

              I will try using dateFormatter andparseDateString()

               

              Thanks!

              • 4. Re: Sort by Date(String) in dataGrid
                Giuseppe Muccioli Level 1

                But when you create the Date object you can also specify the hh:mm:ss with setHours, setMinutes and setSeconds methods.

                Nothing change

                • 5. Re: Sort by Date(String) in dataGrid
                  zakabalt Level 1

                  Yes but I have a String like this (27/01/1999 12:32:57) and I have to know the hh:mm.ss to do the setMinute... And it's more work not? I wanted something to convert a String in date, but if I have to get the hours, minutes and second to de set in the Date...

                   

                  Or probably I don't show and it's easy. If it's simple can you put a example like before but with all the information? (dd/mm/hhhh hh:mm:ss)

                  • 6. Re: Sort by Date(String) in dataGrid
                    Giuseppe Muccioli Level 1

                    I don't know if is simple but:

                    var hms:Array=obj1.theDateField.split(" ")[1].split(":");

                    date1.setHours(hms[0], hms[1], hms[2]);

                    .....

                     

                    But maybe the solution written by NoNickName it's better.

                    Bye

                    • 7. Re: Sort by Date(String) in dataGrid
                      Sebastien V. Level 3

                      I just realized that my solution is probably not directly applicable in your situation. I thought that you had a Date object stored somewhere, but I think I missunderstood that. You just have a String in your model right?

                       

                      So my question would be why do you use a String instead of a Date object to store some Date information?

                       

                      My solution requires you to use a Date object instead of a String, or to convert your String into a Date just like Buzzone's one...

                      • 8. Re: Sort by Date(String) in dataGrid
                        zakabalt Level 1

                        Yes I have only a String. I don't know sure the reason, we work with Java(server), DataBase.. and the only thing that I know is that I receive a String and I need a Date. I have thought that it will be much more easy.

                        • 9. Re: Sort by Date(String) in dataGrid
                          Sebastien V. Level 3

                          If the java back-end has a java.util.Date object, there should be no problem retrieving it as a Date in the Flex front-end, and this would probably save you a lot of troubles, for this issue and for later developpements...

                           

                          Otherwise I don't see any other alternatives than to parse the String, and to build a Date object in the front-end.