2
votes

So I am attempting to cut my CPP teeth on an existing application.

I ran into a bit of a snag. My combobox items are being added in order as you can see below. However, the output is

[1,10,11,12,13,14,15,2,3,4,5,6,7,8,9]

I have looked at the CComboBox documentation here. Yet, I still am confused as to why this is producing this result.

for (int i = 1; i <= m_pPage2->GetNumberColumns(); i++)
{
    CString szColNum;
    szColNum.Format (_T("%d"), i);
    m_cSubColumn.AddString(szColNum);
}
3
Look at those numbers carefully. They are sorted using the ASCII collating sequence, not numerically. If the combo box is sorted, it sorts via ASCII, not numbers.PaulMcKenzie
No, I am sure it's sorting it. I'm not sure why @PaulMcKenzieAnthony Russell
You have to take off the (I think it's called this) CBS_SORT flag from the combo box style. Then the numbers are added as you go along.PaulMcKenzie

3 Answers

3
votes

The standard comparison functions don't deal well with strings containing numbers. They do not take into account that the size of the string should also come into play. With that since "10" starts with a "1" then it will come before anything that has more than a "1" at index 0.

If you were to pad all of your number with leading zeros so that the string sizes were the same it would sort it in the normal numerical order.

To stop the CComboBox from sorting its contents when you use AddString() you need to set the CBS_SORT property to false

2
votes

The issue is that your combo box is using the CBS_SORT style, thus the data is sorted using the ASCII collating sequence.

To turn off the sorting, you have to remove the CBS_SORT style from the combo box. Depending on the resource tool(s) you use, this style can be removed by checking some item in your tool to turn on/off sorting, or go straight to the resource file itself and just remove the CBS_SORT style from the combo box definition.

1
votes

In the Properties window for the Combo Box make sure set is set to False. This will allow your combo box to display the data the way you have it input.

*This is what I do in Visual Studio, I didn't see where you said what IDE you were using.