Solved

Sorting a CListBox box numerically

Posted on 2009-03-30
14
551 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IPhone IOS Development Project Problem 4 28
maxBlock challenge 30 100
matchUp  challenge 6 48
matchUp  challenge 9 71
Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

24 Experts available now in Live!

Get 1:1 Help Now