8 Replies Latest reply on Feb 24, 2011 11:48 AM by DIY_Lobotomy

    Thoughts on sorting...I need some ideas!

    DIY_Lobotomy Level 1

      I have a datagrid component with sortable columns when you click the header of the column that you want sorted.  The problem is that two of the columns contain numbers, so the sorting gets kind of weird.  Like, it would sort the numbers like this:

       

      1

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      2

      20

      21

      22

      23

      3

      4

      5

       

      Obviously this could be fixed by adding leading 0's, but visually I don't want that (mostly because one of the columns are dollar figures, so having something like $00123.46 instead of $123.46 is just odd).

       

      Does anyone have any solutions for fixing this?

       

      Thanks!!!

        • 1. Re: Thoughts on sorting...I need some ideas!
          landar

          By default, a datagrid will sort alphabetically as you are seeing. You want an numeric sort.

          Try setting your column according to your code...heres an example that might help:

           

          var dp:DataProvider = new DataProvider();

          dp.addItem({columnA:"Row 1A", columnB:1234.00});

          dp.addItem({columnA:"Row 2A", columnB:56.30});

          dp.addItem({columnA:"Row 3A", columnB:789.12});

           

          var colA:DataGridColumn = new DataGridColumn("columnA");

          var colB:DataGridColumn = new DataGridColumn("columnB");

           

          colB.sortOptions = Array.NUMERIC;

          • 2. Re: Thoughts on sorting...I need some ideas!
            DIY_Lobotomy Level 1

            Unless I did something wrong, I don't think it worked.  Here's the line I wrote:

             

            topSales.getColumnAt(0).sortOptions = Array.NUMERIC;

             

            Where "topSales" is the name of the datagrid and the first column is the one I want to be numeric.  Either way, it still sorts the same way.  Did I mess this up somehow, or is there another solution?

             

            I also tried this, to no avail:

             

            var first_dgc:DataGridColumn = new DataGridColumn("Rank");
            first_dgc.headerText = "RANK";
            topSales.addColumn(first_dgc);
            
            first_dgc.sortOptions = Array.NUMERIC; 
            

             

            No errors were output...it just didn't work.

            • 3. Re: Thoughts on sorting...I need some ideas!
              DIY_Lobotomy Level 1

              Actually, what I posted above is not totally accurate.  I noticed that the first three lines were within an "if" statement of a function, and the last statement was in another function later on.  So I figured the problem was that the "first_dgc" variable wasn't even being recognized...

               

              ...So I moved the last (4th) line of the code above back in with the rest:

               

              var first_dgc:DataGridColumn = new DataGridColumn("Rank");
              first_dgc.headerText = "RANK";
              first_dgc.sortOptions = Array.NUMERIC; 
              topSales.addColumn(first_dgc);
              

               

              And now I'm getting the error upon output of:

               

              There is no property with the name 'sortOptions'

               

              Hmmmm../

              • 4. Re: Thoughts on sorting...I need some ideas!
                landar Level 1

                I have found a "sortItemsBy" method for dataGrids. You might google that

                and study it. To be honest, I tried it but still get alph sort.


                • 5. Re: Thoughts on sorting...I need some ideas!
                  landar Level 1

                  Ok, I think I found the problem. I needed "import.mx.controls.DataGrid" at

                  the top of my design file.

                   

                  So your design would be something like this:

                   

                  first_dgc.sortColumnsBy("Rank", Array.NUMERIC);

                  • 6. Re: Thoughts on sorting...I need some ideas!
                    DIY_Lobotomy Level 1

                    Okay - now I'm getting the following error:

                     

                    There is no method with the name 'sortColumnsBy'

                     

                    Which kind of makes sense, because you know how when you type a "proper" AS term it is colored blue?  Well 'sortColumnsBy' doesn't turn blue, so it doesn't appear to be a valid method.

                     

                    The only similar thing I could try to not get stopped-up with an output error was this:

                     

                    sortItemsBy("Rank", Array.NUMERIC);

                     

                    But even that didn't actually work.

                    • 7. Re: Thoughts on sorting...I need some ideas!
                      landar Level 1

                      Did you import the datagrid controls lib first?

                       

                      "import.mx.controls.DataGrid"

                      • 8. Re: Thoughts on sorting...I need some ideas!
                        DIY_Lobotomy Level 1

                        Yeah - I put that at the top of the AS.  Well, actually I copy/pasted what you wrote and got an error, but then I took out the first "." after "import" and replaced it with a space.  Then no more errors.  But still no correct sorting.  :-/