Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Combo Box sorting

Combo Box sorting

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionalgorithmscareer
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    liz3
    wrote on last edited by
    #1

    I'm working on MS Visual C++ 6.0. I have a dialog with a combo box on it. Since it needs to display the items in sorted order, i have checked the 'sort' feature available by right-clicking on the combo box and choosing "Sort". This does the job, except that the sorting takes place such that 11 appears before 2 (because the sorting is string based). I need the sorting done based on the length too i.e it should appear as 9, 10 ,11 and not 10, 11 and then 9. How can I edit the "Sort" functionality of the combo box? :^)

    C C L C L 5 Replies Last reply
    0
    • L liz3

      I'm working on MS Visual C++ 6.0. I have a dialog with a combo box on it. Since it needs to display the items in sorted order, i have checked the 'sort' feature available by right-clicking on the combo box and choosing "Sort". This does the job, except that the sorting takes place such that 11 appears before 2 (because the sorting is string based). I need the sorting done based on the length too i.e it should appear as 9, 10 ,11 and not 10, 11 and then 9. How can I edit the "Sort" functionality of the combo box? :^)

      C Offline
      C Offline
      chandu004
      wrote on last edited by
      #2

      what are the values that your combo carry. are they fixed? or are they going to change regularly.

      -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

      L 1 Reply Last reply
      0
      • L liz3

        I'm working on MS Visual C++ 6.0. I have a dialog with a combo box on it. Since it needs to display the items in sorted order, i have checked the 'sort' feature available by right-clicking on the combo box and choosing "Sort". This does the job, except that the sorting takes place such that 11 appears before 2 (because the sorting is string based). I need the sorting done based on the length too i.e it should appear as 9, 10 ,11 and not 10, 11 and then 9. How can I edit the "Sort" functionality of the combo box? :^)

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        AFAIK you should

        • disable the automatic (lexicographic) sort of the combobox.
        • Sort yourself the items on insertion.

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        1 Reply Last reply
        0
        • L liz3

          I'm working on MS Visual C++ 6.0. I have a dialog with a combo box on it. Since it needs to display the items in sorted order, i have checked the 'sort' feature available by right-clicking on the combo box and choosing "Sort". This does the job, except that the sorting takes place such that 11 appears before 2 (because the sorting is string based). I need the sorting done based on the length too i.e it should appear as 9, 10 ,11 and not 10, 11 and then 9. How can I edit the "Sort" functionality of the combo box? :^)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          This is a common problem with listbox types that sort on string values and do not take account of numbers. I think the only way to get your items in the correct order is to sort them manually and then add them to an unsorted combobox.

          It's time for a new signature.

          1 Reply Last reply
          0
          • L liz3

            I'm working on MS Visual C++ 6.0. I have a dialog with a combo box on it. Since it needs to display the items in sorted order, i have checked the 'sort' feature available by right-clicking on the combo box and choosing "Sort". This does the job, except that the sorting takes place such that 11 appears before 2 (because the sorting is string based). I need the sorting done based on the length too i.e it should appear as 9, 10 ,11 and not 10, 11 and then 9. How can I edit the "Sort" functionality of the combo box? :^)

            C Offline
            C Offline
            Cool_Dev
            wrote on last edited by
            #5

            For a fixed owner draw combo box, handle WM_COMPAREITEM of parent dialog. Or create your own CComboBox derived class and over-ride CComboBox::CompareItem(). Implement your comparison logic there :) CComboBox::CompareItem WM_COMPAREITEM Message

            1 Reply Last reply
            0
            • C chandu004

              what are the values that your combo carry. are they fixed? or are they going to change regularly.

              -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

              L Offline
              L Offline
              liz3
              wrote on last edited by
              #6

              they are not fixed. Will change regularly.

              C 1 Reply Last reply
              0
              • L liz3

                they are not fixed. Will change regularly.

                C Offline
                C Offline
                chandu004
                wrote on last edited by
                #7

                in that case, follow what experts said below. i.e. you have to sort them manually.

                liz3 wrote:

                they are not fixed. Will change regularly.

                it would be good, if you can narrate your task, with an example. like, 1.are they going to have only numeric values? or can it have text too. 2.will it be loaded at the initiation of the application? or in between while operation on the UI. 3.please narrate what are the different possible values and what are the conditions, at which the values get addd to the combo. all the best.

                -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                1 Reply Last reply
                0
                • L liz3

                  I'm working on MS Visual C++ 6.0. I have a dialog with a combo box on it. Since it needs to display the items in sorted order, i have checked the 'sort' feature available by right-clicking on the combo box and choosing "Sort". This does the job, except that the sorting takes place such that 11 appears before 2 (because the sorting is string based). I need the sorting done based on the length too i.e it should appear as 9, 10 ,11 and not 10, 11 and then 9. How can I edit the "Sort" functionality of the combo box? :^)

                  L Offline
                  L Offline
                  loyal ginger
                  wrote on last edited by
                  #8

                  In addition to the above suggestions, I offer the following. If you don't mind the items of the combo box items look a bit different, and also you know the maximum width of the numbers, you can pad the items with leading zeros. This way, with the "sort" property set to true, the items will sort correctly. For example, if you know the maximum width will be two, then format the items like this: 02, 09, 11, 10, etc. Hope this helps.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups