Solved

# Sorting CComboBox containing numbers

Posted on 2002-07-22
475 Views
Hi,
I have a CCombobox containing numbers, I want its items to be sorted.
How do I do that ?

thanks,
iris.
0
Question by:irisz
• 2
• 2
• 2
• +2

LVL 86

Expert Comment

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

My combobox contains numbers !!!
I need it to be sorted not alpha-betic, but numerically !!!
0

LVL 86

Expert Comment

>>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 30

Expert Comment

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

LVL 8

Expert Comment

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

VinExpert earned 50 total points
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.

{
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]);
}

// 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

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

Sorry i said listbox but i want to sai combobox
0

## Featured Post

### Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…