Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 599
  • Last Modified:

Sorting a CListBox box numerically

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
Wanting2LearnMan
Asked:
Wanting2LearnMan
  • 7
  • 4
  • 3
10 Solutions
 
DanRollinsCommented:
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Wanting2LearnManAuthor Commented:
DanRollins, can you provide a more complex sorting function please?

0
 
AndyAinscowCommented:
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
 
Wanting2LearnManAuthor Commented:
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
0
 
Wanting2LearnManAuthor Commented:
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
 
AndyAinscowCommented:
>>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
 
DanRollinsCommented:
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
 
Wanting2LearnManAuthor Commented:
Dan, I would be very grateful if you could provide this step-by-step for me.  Thanks.
0
 
AndyAinscowCommented:
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
 
DanRollinsCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now