Solved

Sorting CComboBox containing numbers

Posted on 2002-07-22
8
476 Views
Last Modified: 2013-11-20
Hi,
I have a CCombobox containing numbers, I want its items to be sorted.
How do I do that ?

thanks,
iris.
0
Comment
Question by:irisz
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7169455
Add the 'CBS_SORT' sytel to the combo box. You can do that either in the resource editor or by setting that style bit when calling 'Create()'
0
 

Author Comment

by:irisz
ID: 7169476
My combobox contains numbers !!!
I need it to be sorted not alpha-betic, but numerically !!!
0
 
LVL 86

Expert Comment

by:jkr
ID: 7169529
>>My combobox contains numbers !!!

I got that.

>>I need it to be sorted not alpha-betic, but numerically !!!

Where's the difference? Have you tried it?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 7169706
jkr, difference is i.e.

Alphabetical:
23
234
24

Numerical:
23
24
234

irisz, best would be to first fill i.e. an array with the
numbers, sort the array (i.e. with qsort) and then fill
the combobox from the array.

ZOPPO
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:VinExpert
ID: 7169731
Hi irisz,

I understand what u r talking about. Yes, there is no straight forward way I believe. The real simple way would be Remove the "Sorting" option to that combobox and insert the items in a sorted manner. As u have numbers, take them in a array and sort that array first and then insert the items.

Try it out.
VinExpert
0
 
LVL 8

Accepted Solution

by:
VinExpert earned 50 total points
ID: 7169809
Hi,

Just see the following two functions. U can derive ur own class from CComboBox and add the following functions and call the AddNumber function. Just test the code once for correct working.

int CSortCombo::AddNumber(int nItem, BOOL bSelect)
{
     CString cStrTemp;
     int nDummy = 0;
     int nIndex = 0;
     int nItemCnt = GetCount();
     int *pItems = new int[nItemCnt + 2];
     
     ZeroMemory(pItems, (nItemCnt + 2) * sizeof(int));

     // Get the items
     if(nItemCnt != 0)
     {
          for(nIndex = 0; nIndex < nItemCnt; nIndex++)
          {
               GetLBText(nIndex, cStrTemp);
               sscanf(cStrTemp, "%d", &nDummy);
               pItems[nIndex] = nDummy;
          }
     }

     // Add the new eliment also
     pItems[nIndex] = nItem;
     nItemCnt++;

     // Sort them
     if(nItemCnt > 1)
          SortNumbers(pItems, nItemCnt);

     // Clear the previous items
     ResetContent();

     // Insert the sorted items now
     for(nIndex = 0; nIndex < nItemCnt; nIndex++)
     {
          cStrTemp.Format("%d", pItems[nIndex]);
          AddString(cStrTemp);
     }

     // Clean the memory
     if(pItems)
     {
          delete []pItems;
          pItems = NULL;
     }

     // Get the index of insert item
     cStrTemp.Format("%d", nItem);
     nIndex = FindStringExact(0, cStrTemp);

     // Select the inserted string
     if(bSelect)
     {
          SetCurSel(nIndex);
     }

     return nIndex;
}

void CSortCombo::SortNumbers(int *pElements, int nCount)
{
     for(int nMain = 0; nMain < nCount; nMain++)
     {
          for(int nSub = nMain + 1; nSub < nCount; nSub++)
          {
               if(pElements[nMain] > pElements[nSub])
               {
                    int nTemp = pElements[nMain];
                    pElements[nMain] = pElements[nSub];
                    pElements[nSub] = nTemp;
               }
          }
     }
}

Hope it helps.
VinExpert
0
 

Expert Comment

by:Mortiis1
ID: 12672204
I think it's a little slow to sort each time you want to insert a number.
If you want to insert 500 number it could be better to make 500 addstring
and after sort the listbox.

Or if you have the number sort them and after insert them.
0
 

Expert Comment

by:Mortiis1
ID: 12672208
Sorry i said listbox but i want to sai combobox
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 107
sum13 challenge 24 91
fizzArray2 challenge 1 72
changeXy challenge 13 69
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now