Solved

Sorting a CListBox box numerically

Posted on 2009-03-30
14
565 Views
Last Modified: 2013-12-21
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
Comment
Question by:Wanting2LearnMan
  • 7
  • 4
  • 3
14 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 150 total points
ID: 24024570
The easiest solution is to insert a 0 (zero) in front of the single-digit numbers.
istboxcontrol.AddString("01 PERSONS NAME1");
istboxcontrol.AddString("02 PERSONS NAME2");
...
istboxcontrol.AddString("10 PERSONS NAME10");
istboxcontrol.AddString("11 PERSONS NAME11");
 
I can provide a more complex one with custom sorting function if you express interest,
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 350 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

by:AndyAinscow
AndyAinscow earned 350 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

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

0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 350 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

by:Wanting2LearnMan
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

by:AndyAinscow
AndyAinscow earned 350 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

by:AndyAinscow
AndyAinscow earned 350 total points
ID: 24026737
0
 

Author Comment

by:Wanting2LearnMan
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

by:AndyAinscow
AndyAinscow earned 350 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

by:DanRollins
DanRollins earned 150 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

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

Assisted Solution

by:AndyAinscow
AndyAinscow earned 350 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

by:DanRollins
DanRollins earned 150 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

With Windows Embedded Handheld, called Windows Mobile, Microsoft re-designed the user interface. The Start Icon moved down to the bottom, inside the menu bar area.   If you need to hide the Start Icon and/or the SIP (soft input panel, softwar…
If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

808 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