9 Replies Latest reply on Apr 27, 2009 10:33 AM by Flex harUI

    Sort Items Alphabetically in List Control

    alice_data Level 1

      Hi,

       

        I have a code snippet as in the following: (input is a dynamically generated array based on users selection)

       

        //Create a new List element
                            list1 = new List();              
                                                 
                            var sort:Sort = new Sort();
                            sort.fields = [new SortField(null,true)];
                            input.sort = sort;  # 1168: Illegal assignment to function sort.
                            input.refresh();
                            list1.dataProvider = input;    
                            list1.setStyle("color","blue");     
                            list1.setStyle("borderStyle","none");
                            list1.labelField = "name";
                            list1.allowMultipleSelection = false;
                            list1.doubleClickEnabled = true;         
                                                
                            list1.addEventListener("change",updateNameSelected2); //Add Event Listener on single Click
                            list1.addEventListener("doubleClick",appointmentList2); //Add Event Listener on DoubleClick

       

        I don't think this seems to be legal, because I keep getting the error #1168 at the line where I marked input.sort= sort;

        I have a question here, when the list in Flex is generated, does it always show according to how the xml output is printed out, or does it generate the results printing alphabetically by default? If the former, why would I get the #1168 error, and how could I fix it?

       

      Thanks in advance.

       

      Alice

        • 1. Re: Sort Items Alphabetically in List Control
          Barna Biro Level 3

          Hi Alice,

           

          I'm not sure what "input" is or of which type it is but did you try sorting the dataProvider directly? Simply call the custom sort method on lits1.dataProvider.sort = sort; and don't forget to refresh the provider once sorted with list1.dataProvider.refresh(); I don't see a reason why this would not work.

           

          By the way, you are getting the #1168 Error because it seems that your object named "input" does not support such a function. In case your object called "input" is not of type Array or ArrayCollection then that might be the reason of the error.

           

          Best regards,

          Barna Biro

          1 person found this helpful
          • 2. Re: Sort Items Alphabetically in List Control
            alice_data Level 1

            Hi,

             

            Yeah, I found out that it seems like I have to use an ICollectionView for this, and the code is sort of working now:

             

            //Create a new List element
                                  list1 = new List();        
                                  var col:ICollectionView = new ArrayCollection(input);                     
                                  var sort:Sort = new Sort();
                                  sort.fields = [new SortField(null,true)];
                                  col.sort= sort;
                                  col.refresh();
                                  list1.dataProvider = col;    
                                  list1.setStyle("color","blue");     
                                  list1.setStyle("borderStyle","none");
                                  list1.labelField = "name";
                                  list1.allowMultipleSelection = false;
                                  list1.doubleClickEnabled = true;  
                                  list1.height = 325;
                                  list1.width = 200;                
                                  list1.addEventListener("change",updateNameSelected2); //Add Event Listener on single Click
                                  list1.addEventListener("doubleClick",appointmentList2); //Add Event Listener on DoubleClick

             

            Referring to the question you asked, the array input contains text and numeric values, so it looks something like abc-123m. It looks like the lettering part order is now accurate, but how could I get the numeric part to be sorted too?

             

            Thanks again for your help.

             

            Alice

            • 3. Re: Sort Items Alphabetically in List Control
              Barna Biro Level 3

              Glad to hear you solved the problem. For the numeric sort you can simply specify that you are about to sort numeric data with the help of the "numeric" proprty that signals that the data you are about to sort is numeric and not text.

               

              http://www.igorcosta.com/flex3/doc/mx/collections/SortField.html

               

              Best regards,

              Barna Biro

              • 4. Re: Sort Items Alphabetically in List Control
                alice_data Level 1

                Hi,

                 

                  This is what I changed:

                 

                       sort.fields = [new SortField(null,true,false,true)];  

                 

                  Is this what you are suggesting?

                  I still don't see anything different the way the information has been sorted than before, I still get something like:

                 

                  abc-130m

                  abc-129p

                  abc-444a

                 

                  when I wanted to have it like

                 

                  abc-129p

                  abc-130m

                  abc-444a

                 

                  Is this possible?

                 

                Thanks for your help.

                 

                Alice

                • 5. Re: Sort Items Alphabetically in List Control
                  Barna Biro Level 3

                  Ohh, sorry, if that's what you wanted then I understood something totally different. The short answer for your question would be: it is possible but not with the default methods that Adobe offers. In order to do such a sort, you somehow have to break your data appart ( maybe store the string part in a list and store the numeric part in different list ) and find a way to somehow link the two list together and sort them so they are displayed correctly.

                   

                  I really don't think it's possible to do a such a sort with the default methods you get form the Sort class, at least, I'm quite sure that there's no easy solution for this. The best idea that comes to my mind is to slit the data in half, store the string part and store the numeric part and make a sort on both of the arrays ( like you would do with the DataGrid columns, example: you can sort a grid column by using 2 conditions, maybe "name" and "size"; this would be done by simply adding 2 SortFields to your sort method, one that would first sort the data depending on it's "name" and the second that would take the "name" in consideration and sort the data again by "size" ).

                   

                  Sorry, that's the best I can do at the moment.

                   

                  Wishing you luck,

                  Barna Biro

                  • 6. Re: Sort Items Alphabetically in List Control
                    Michael Borbor Level 4

                    If you do something like this I think it'll work

                     

                    var mySort:Sort:Sort=new Sort()

                    var mySortField:SortField;

                    mySortField= new SortField(null,false,false)

                    mySort.fields=[mySortField]

                    myAC.sort=mySort

                    myAC.refresh()

                    Sincerely,

                     

                    Michael

                     

                    El 27/04/2009, a las 9:00, alice_data <forums@adobe.com> escribió:

                     

                    >

                    Hi,

                    >

                      This is what I changed:

                    >

                           sort.fields = ;

                    >

                      Is this what you are suggesting?

                      I still don't see anything different the way the information has 

                    been sorted than before, I still get something like:

                    >

                      abc-130m

                      abc-129p

                      abc-444a

                    >

                      when I wanted to have it like

                    >

                      abc-129p

                      abc-130m

                      abc-444a

                    >

                      Is this possible?

                    >

                    Thanks for your help.

                    >

                    Alice

                    >

                    • 7. Re: Sort Items Alphabetically in List Control
                      alice_data Level 1

                      Hi, Mike:

                       

                        This is working so much better. But, I have one question, If I have something  like this, then the results get screwed up:

                       

                        abc-1m

                        abc-101m

                        abc-121m

                       

                        The output of the ordering:

                       

                        abc-101m

                        abc-121m

                        abc-1m

                       

                        Is this what the ordering is supposed to do? Or, is there something else I have done wrong here by applying your code directly? Here is the code:

                       

                          list1 = new List();  //Create a new List element                     
                                            var col:ICollectionView = new ArrayCollection(input);                     
                                            var sort:Sort = new Sort();
                                            sort.fields = [new SortField(null,false,false)];           
                                            col.sort= sort;                  
                                            col.refresh();
                                            list1.dataProvider = col;    
                                            list1.setStyle("color","blue");     
                                            list1.setStyle("borderStyle","none");
                                            list1.labelField = "name";
                                            list1.allowMultipleSelection = false;
                                            list1.doubleClickEnabled = true;                                              
                                            list1.width = 200;   
                                            list1.height = 295;                             
                                            list1.addEventListener("change",updateNameSelected2); //Add Event Listener on single Click
                                            list1.addEventListener("doubleClick",appointmentList2); //Add Event Listener on DoubleClick

                       

                      Thanks for your help.

                       

                      Alice

                      • 8. Re: Sort Items Alphabetically in List Control
                        Michael Borbor Level 4

                        That's the expected behavior of the SortField, I don't think that 

                        using mix content you could accomplish what you want, remember the 

                        sorting order symbols, numbers, uppercase, lowercase.

                         

                        Sincerely,

                         

                        Michael

                         

                        El 27/04/2009, a las 9:47, alice_data <forums@adobe.com> escribió:

                         

                        >

                        Hi, Mike:

                        >

                          This is working so much better. But, I have one question, If I 

                        have something  like this, then the results get screwed up:

                        >

                          abc-1m

                          abc-101m

                          abc-121m

                        >

                          The output of the ordering:

                        >

                          abc-101m

                          abc-121m

                          abc-1m

                        >

                          Is this what the ordering is supposed to do? Or, is there 

                        something else I have done wrong here by applying your code 

                        directly? Here is the code:

                        >

                            list1 = new List();  //Create a new List element

                                              var col:ICollectionView = new 

                        ArrayCollection(input);

                                              var sort:Sort = new Sort();

                                              sort.fields = ;

                                              col.sort= sort;

                                              col.refresh();

                                              list1.dataProvider = col;

                                              list1.setStyle("color","blue");

                                              list1.setStyle("borderStyle","none");

                                              list1.labelField = "name";

                                              list1.allowMultipleSelection = false;

                                              list1.doubleClickEnabled = true;

                                              list1.width = 200;

                                              list1.height = 295;

                                                

                        list1.addEventListener("change",updateNameSelected2); //Add Event 

                        Listener on single Click

                                                

                        list1.addEventListener("doubleClick",appointmentList2); //Add Event 

                        Listener on DoubleClick

                        >

                        Thanks for your help.

                        >

                        Alice

                        >

                        • 9. Re: Sort Items Alphabetically in List Control
                          Flex harUI Adobe Employee

                          Michael is correct.  You will need to supply a custom sortCompareFunction that strips off the can pick the numbers out of the string pattern.

                           

                          Alex Harui

                          Flex SDK Developer

                          Adobe Systems Inc.

                          Blog: http://blogs.adobe.com/aharui