Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Sorting a CListBox box numerically

Posted on 2009-03-30
Medium Priority
596 Views
I am using eVC++4

I'm populating a CListBox control by doing this:
istboxcontrol.AddString(" 1 PERSONS NAME1");
istboxcontrol.AddString(" 2 PERSONS NAME2");
istboxcontrol.AddString(" 3 PERSONS NAME3");
istboxcontrol.AddString(" .. PERSONS NAME...");
istboxcontrol.AddString(" 10 PERSONS NAME10");
istboxcontrol.AddString(" 11 PERSONS NAME11");
istboxcontrol.AddString(" 12 PERSONS NAME12");
etc..

I want the names to be sorted numerically according to the starting numbers but if I tick the 'sort' box in the controls properties they are sorted like so:
" 1 PERSONS NAME1"
" 10 PERSONS NAME10"
" 11 PERSONS NAME11"
" 12 PERSONS NAME12"
" 2 PERSONS NAME2"
" 3 PERSONS NAME3"
etc...

How can I sort these properly?
0
Question by:Wanting2LearnMan
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 7
• 4
• 3

LVL 49

Accepted Solution

DanRollins earned 600 total points
ID: 24024570
The easiest solution is to insert a 0 (zero) in front of the single-digit numbers.
...

I can provide a more complex one with custom sorting function if you express interest,
0

LVL 44

Assisted Solution

AndyAinscow earned 1400 total points
ID: 24026197
The sorting is alphabetic.
Solution.
Deselect the sort option in the list box.  Sort them before ading to the list box then as you add them they will be in the correct order - as you have done so already.

so
istboxcontrol.AddString(" 1 PERSONS NAME1");
istboxcontrol.AddString(" 2 PERSONS NAME2");
istboxcontrol.AddString(" 3 PERSONS NAME3");
istboxcontrol.AddString(" .. PERSONS NAME...");
istboxcontrol.AddString(" 10 PERSONS NAME10");
istboxcontrol.AddString(" 11 PERSONS NAME11");
istboxcontrol.AddString(" 12 PERSONS NAME12");
will appear how you want them to.

0

LVL 44

Assisted Solution

AndyAinscow earned 1400 total points
ID: 24026509
ps. If you don't know how to sort and the info comes from a database then just a sort clause in the query.
eg  instead of 'SELECT * FROM Adress' use 'SELECT * FROM Adress ORDER BY PersonName'
0

Author Comment

ID: 24026627
DanRollins, can you provide a more complex sorting function please?

0

LVL 44

Assisted Solution

AndyAinscow earned 1400 total points
ID: 24026659
Why not just enter keywords like
sort string function MFC
into the search option here at EE and then look at the hits such as
http://www.experts-exchange.com/Programming/System/Windows__Programming/MFC/Q_11651559.html?sfQueryTermInfo=1+10+function+mfc+sort+string
which was the second one found.
0

Author Comment

ID: 24026678
AndyAinsciw, I like your idea but I allow the user to change the numbers of the people.  So I would need a ay to resort them every time a number has changed.
0

LVL 44

Assisted Solution

AndyAinscow earned 1400 total points
ID: 24026707
Well just inserting a string would result in duplicate numbers so you would have to empty and refill the listbox each time so what is the problem?

Initially - empty listbox, sort list of items, fill list box.

User adds a new person to the list.
then you need this code - empty listbox, sort list of items, fill list box.
erm deja vu !
0

LVL 44

Assisted Solution

AndyAinscow earned 1400 total points
ID: 24026737
0

Author Comment

ID: 24026802
Thanks, it seems that the only way to easily sort numerically is to add a 0 before the 1,2,3 etc. is this right?
0

LVL 44

Assisted Solution

AndyAinscow earned 1400 total points
ID: 24026858
>>Thanks, it seems that the only way to easily sort numerically is to add a 0 before the 1,2,3 etc. is this right?
NO.

If you compare strings then you get an alphabetic sort.  1, 10, 11, 2 ....
If you compare numbers you get a numeric sort. 1, 2, 10, 11...

Deselect the sort option in the list box.  Sort them before ading to the list box then as you add them they will be in the correct order
0

LVL 49

Assisted Solution

DanRollins earned 600 total points
ID: 24033642
Implementing a CListBox comparison function is pretty straightforward, but it does require deriving a class and there are a few slightly complicated steps.
Before doing that, let's make sure that it will do the job for you. Once a CListBox::CompareItem() fn is implemented, all it will do is decide where newly-added items will appear in the list.
If the sorting key (Person Number) is changed in place, the item won't automatically move to the right place. You would need to delete the item and add it back to the list.
Another reason to add this complication (custom sorting) is if you need to reverse the sort order or sort by a different criteria (say, person's age). But with CListBox, such operations require removal and re-adding all items...
So, I agree with AndyAinscow -- With CListBox, you might as well sort the items before you insert them. It's a different story with CListCtrl -- the more commonly-used "list box" these days;  the older CListBox is more limited.
If you still want to see how to add a custom sorting function for CListBox, I'll provide the step-by-step for you.
-- Dan
0

Author Comment

ID: 24038027
Dan, I would be very grateful if you could provide this step-by-step for me.  Thanks.
0

LVL 44

Assisted Solution

AndyAinscow earned 1400 total points
ID: 24043818
What is so difficult, comparing two numbers is so trivial that I can hardly believe you don't know how to do it?

int x = 1;
int y = 2;
if(x < y)
DoLessThanAction();   // as 1 is less than 2 this will be called in this case
else
DoGreaterThanAction();

ps. I provided the link to the CompareItem for you to look at, it doesn't mean it is suitable for your case.  As I said earlier, sort the list yourself then add to the lstbox is likely to be the 'best' for you.
0

LVL 49

Assisted Solution

DanRollins earned 600 total points
ID: 24076405
I was going to show you how to implement the CListBox::CompareItem, but it turns out to be a signficant headache.  In order to get the WM_COMPAREITEM messages, the control must be set to Owner Draw and that means that you need to supply drawing and measuring logic as well as the comparison function.
It's really not worth it... for at least two reasons:
1) you might s well sort the strings before you Add them -- you get the same effect without the hassle.
2) The CListCtl (in Report View) is the more modern version of a "list box"  and is almost universally used these days.  It is easy to implement custom sorting in that control.  See:

CListCtrl Class
http://msdn.microsoft.com/en-us/library/hfshke78(VS.80).aspx
Probably everything you need to know is here:
I feel sort of...unsorted! A Guide to Sorting, including sorting CListCtrl, CComboBox, and CListBox
http://www.flounder.com/sorting.htm
0

## Featured Post

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this feâ€¦
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as formâ€¦
###### Suggested Courses
Course of the Month5 days, 20 hours left to enroll

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

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