Sort ListBox (Ascending / Descending)?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • mafranks
    Forum Member
    • Jul 2004
    • 19

    Sort ListBox (Ascending / Descending)?

    I have a ListBox populated with file names read from a directory folder. Does anyone know of a way to added a button/s that will sort the ListBox in ascending or descending order? If not, it may be something to look at adding to future ListBox Settings.
  • TJ_Tigger
    Indigo Rose Customer
    • Sep 2002
    • 3159

    #2
    My suggestion would be to read the contents of the listbox into a table and then use the Table.Sort action to sort the table as appropriate and then rewrite the table back into the listbox. the help file gives a function on reverse sort as well.
    TJ-Tigger
    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
    "Draco dormiens nunquam titillandus."
    Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

    Comment

    • mafranks
      Forum Member
      • Jul 2004
      • 19

      #3
      Thanks Tigger. I'll give that a try.

      Comment

      • mafranks
        Forum Member
        • Jul 2004
        • 19

        #4
        Tigger, I did as you suggested. Works great but one needs to remember to uncheck the 'sort' in the ListBox settings as it will take precedence over any reverse sort code the is written. Thanks for your suggestion.

        Comment

        • Adam
          Indigo Rose Staff Member
          • May 2000
          • 2149

          #5
          Tiggers suggestions are G-r-r-r-r-r-eat!

          Comment

          • Intrigued
            Indigo Rose Customer
            • Dec 2003
            • 6138

            #6
            TJ-Tigger... definitely the #1 go-to guy for Table(s) talk!

            His explanations helped to clear up my take on multi-dimensional arrays!

            U'sin.. gooood peoples 'tig!
            Intrigued

            Comment

            • TJ_Tigger
              Indigo Rose Customer
              • Sep 2002
              • 3159

              #7
              Originally posted by mafranks
              Tigger, I did as you suggested. Works great but one needs to remember to uncheck the 'sort' in the ListBox settings as it will take precedence over any reverse sort code the is written. Thanks for your suggestion.
              Good suggestion on the 'sort' checkbox. I would have forgotten about it and then beaten my head against the wall trying to figure out what went wrong.

              Would you be willing/able to post your code/functions for others benefit?

              Tigg
              TJ-Tigger
              "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
              "Draco dormiens nunquam titillandus."
              Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

              Comment

              • mafranks
                Forum Member
                • Jul 2004
                • 19

                #8
                This is one page of many that make up an e-Safety Manual for use throughout our manufacturing plant. On this page the user is presented with a list of minutes from a safety committee. The minutes are kept on the company's networked drive. The file names for the minutes are always in the format: 'yyyy-mm-dd'. Therefore when a reverse (descending) sort is executed prior to filling the ListBox, the minutes show the latest first so the viewer does not have to scroll down several years worth of monthly meeting minutes to get the latest minutes.

                For the descending sort to work, the Sort checkbox in the ListBox settings must be unchecked. If you wanted to do an ascending sort, simply check the Sort checkbox and don't wory about any addtional coding. I suppose sort buttons could be added to the page if you wanted to sort either way.

                I cannot take credit for all of this code. Much of it was suggestions from the members of AMS Forums. Thanks everybody. Soooo for the code:

                --Find files on Network Drive for PSRB Committee
                files = File.Find("\\\\usldcvs1fsh02\\lnlccf20$\\SAFETY\\S afety Committees\\Minutes of All Safety Committees\\PSRB", "*.*", false, false, nil);

                if files ~= nil then
                min = 1;
                max = Table.Count(files);
                ListBox.DeleteItem("ListBox1", LB_ALLITEMS)

                -- Sort files by decending order
                function sorter(v1,v2)
                if (v1 > v2)then
                return true;
                else
                return false;
                end
                end

                --Sort files and add to ListBox
                Table.Sort(files, sorter);
                for count = min, max do -- count will increase from 1 to the number of files
                split = String.SplitPath(files[count]); -- split is a variable that will hold the current 'files' path
                result = ListBox.AddItem("ListBox1", split.Filename..split.Extension, files[count]); -- and at the same time the current 'index' and 'data' elements will be added into the ListBox object.
                end

                --Do this if no files are found in folder
                else
                Dialog.Message("Alert!", "File(s) not found!");
                ListBox.SetEnabled("ListBox1", false);
                end

                Comment

                • TJ_Tigger
                  Indigo Rose Customer
                  • Sep 2002
                  • 3159

                  #9
                  Great Thanks. Always nice to see how things ended in a post.

                  Tigg
                  TJ-Tigger
                  "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
                  "Draco dormiens nunquam titillandus."
                  Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

                  Comment

                  • cosmic
                    Forum Member
                    • Oct 2005
                    • 11

                    #10
                    sort mm/dd/yyyy

                    I tried using the sorter function to sort records in my listbox by date and it returns something like

                    10/2/2005
                    2/6/2005
                    3/8/2005
                    5/5/2005
                    8/9/2005
                    8/9/2005

                    any suggesstions of getting this "sorted" out?

                    Comment

                    • Worm
                      Indigo Rose Customer
                      • Jul 2002
                      • 3971

                      #11
                      I'm going to start sounding very redundant here...

                      If you use the DataGrid plugin, you can sort either way by clicing on the column header. Having a column that holds the date in ISO format will sort properly.

                      Comment

                      • TJ_Tigger
                        Indigo Rose Customer
                        • Sep 2002
                        • 3159

                        #12
                        DataGrid be good.

                        You could always use functions found on this site to convert the Gregorian date to Julian and sort using a julian date and then convert back to Gregorian and put back into the LB.

                        Just a thought.
                        TJ-Tigger
                        "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
                        "Draco dormiens nunquam titillandus."
                        Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

                        Comment

                        • Worm
                          Indigo Rose Customer
                          • Jul 2002
                          • 3971

                          #13
                          even using the ISO date would work.

                          Comment

                          • cosmic
                            Forum Member
                            • Oct 2005
                            • 11

                            #14
                            Switched to ISO

                            Ok, so I've switched the date to ISO but is still not sorting in descending order. It sorts but ascending...such as

                            1998/08/02
                            2005/05/15
                            2005/08/28
                            2005/10/30

                            My code is:

                            tpog2site = SQLite.QueryToTable (db2, "Select * from "..srcdb.." where SITE ="..answer);

                            function sorter(v1,v2)
                            if (v1 > v2)then
                            return true;
                            else
                            return false;
                            end
                            end

                            Table.Sort(tpog2site, sorter);


                            Could it possibly be the code that populates the Listbox..

                            for b = 1, tpog2site.Rows do
                            ListBox.AddItem ("ListBox1", tpog2site.Data[b]["LASTPUBLISHEDDATE"].."\t"..tpog2site.Data[b]["S_POG_NAME"].."\t"..tpog2site.Data[b]["BASE_POG_NAME"].."\t"..tpog2site.Data[b]["POG_CODE_DESC_ENG"], tpog2site.Data[b]["POG_CODE_DESC_ENG"]);
                            end


                            Any way of doing it without the plugin????

                            Comment

                            • Worm
                              Indigo Rose Customer
                              • Jul 2002
                              • 3971

                              #15
                              Run the for loop in reverse

                              Code:
                              for b = tpog2site.Rows, 1, -1 do
                              ListBox.AddItem ("ListBox1", tpog2site.Data[b]["LASTPUBLISHEDDATE"].."\t"..tpog2site.Data[b]["S_POG_NAME"].."\t"..tpog2site.Data[b]["BASE_POG_NAME"].."\t"..tpog2site.Data[b]["POG_CODE_DESC_ENG"], tpog2site.Data[b]["POG_CODE_DESC_ENG"]);
                              end
                              Last edited by Worm; 10-26-2005, 01:07 PM.

                              Comment

                              Working...
                              X