5 Replies Latest reply on Jan 2, 2009 11:33 PM by ntsiii

    How to turn a string into a date value

    bitwyse Level 1
      Hi All,

      What is the best way to approach a date sorting issue I have? A web service that I consume returns a date column in a string format. Thus when the user clicks the column to sort a data grid by that date value it sorts incorrectly.

      I realize I could put a sort() on the grid or convert the data somehow. I am hoping several people can give me what would be considered a "best practice" from a code readability and performance point of view.

      Thannks in advance for your thoughts on this issue.
        • 1. Re: How to turn a string into a date value
          levancho Level 3
          if dataGrid's dataprovider is ArayCollection I would use built in Sort feature,
          you will need : Sort class SortField class and compareFunction
          I think I have some example at home will post tonight
          from SDK :
          var col:ICollectionView = new ArrayCollection();
          // In the real world, the collection would have more than one item.
          col.addItem({first:"Anders", last:"Dickerson"});
          // Create the Sort instance.
          var sort:Sort = new Sort();
          // Set the sort field; sort on the last name first, first name second.
          // Both fields are case-insensitive.
          sort.fields = [new SortField("last",true), new SortField("first",true)];
          // Assign the Sort object to the view.
          col.sort = sort;
          // Apply the sort to the collection.
          col.refresh();
          • 2. Re: How to turn a string into a date value
            bitwyse Level 1
            Hi Levancho,

            Thanks for the quick reply.

            Couple questions . ..
            How does this affect my data grid when someone clicks on a column heading.
            Am I better off converting the data once from string to a date and letting the d/grid handle the sorting itself or converting it in the sort routine?
            • 3. How to turn a string into a date value
              levancho Level 3
              you dont have to convert anything just implement function and asign it to Sort object's "compareFunction" property

              The compare function must have the following signature:


              function [name](a:Object, b:Object, fields:Array=null):int


              This function must return the following

              * -1, if a should appear before b in the sorted sequence
              * 0, if a equals b
              * 1, if a should appear after b in the sorted sequence


              inside that function you might need a tmp conversion but overall that should be it

              for example (function can look like this :

              if a===b) return 0;
              if(myStringToDateConvertor(a) > myStringToDateConvertor(m) ) return 1;
              if(myStringToDateConvertor(a) < myStringToDateConvertor(m) ) return -1;
              return 0;
              (remember sortField also has has compareFunction property but you dont need that one).
              • 4. Re: How to turn a string into a date value
                ajesing
                Hi,

                Could someone provide a detailed example with code.

                Appreciate your help.