Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting columns in a listView by number, not string

Status
Not open for further replies.

StevoSimpson

Programmer
Apr 23, 2006
6
0
0
GB
Hi,

I've managed to create a listView which can sort the columns in ascending or descending order by following the MS help file:
However, this sorts by string, so any numbers, for example in an ID field, dont get sorted correctly - instead of 0, 1,2,3,4,5 etc, I get: 0,1,10,100,101 etc.

In the hepl file, it says that to compare by a different means, ie. integers, etc, then just change this code:
Code:
ObjectCompare->Compare(listviewX->SubItems->Item[ColumnToSort]->Text,listviewY->SubItems->Item[ColumnToSort]->Text);

However, it doesn't say what to change it to, and I'm not entirely sure how to do this. Does anyone have any ideas?

Cheers.
 
I would guess it would be as simple as this:
Code:
compareResult = (atoi( listviewX->SubItems->Item[ColumnToSort]->Text ) < atoi( listviewY->SubItems->Item[ColumnToSort]->Text ));
 
Oops, I should have said this:
Code:
int numX = atoi( listviewX->SubItems->Item[ColumnToSort]->Text );
int numY = atoi( listviewY->SubItems->Item[ColumnToSort]->Text );

if ( numX < numY )
{
   compareResult = -1;
}
else if ( numY > numX )
{
   compareResult = 1;
}
else
{
   compareResult = 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top