Solved

Sorting a CListBox box numerically

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Contraction monitoring App 2 80
Unix Command -- Challenging  question 7 96
compass android app not working on moto 4g android phone 5 38
I could not set window to top 4 21
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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