By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,988 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,988 IT Pros & Developers. It's quick & easy.

Listview - Simple code but sorts in different format

100+
P: 103
The following code is for sorting,but that works in a different format:
Expand|Select|Wrap|Line Numbers
  1.  Private Sub ListView1_ColumnClick(ByVal ColumnHeader As   MSComctlLib.ColumnHeader)
  2.  With ListView1  '// change to the name of the list view
  3.  Static iLast As Integer, iCur As Integer
  4.      .Sorted = True
  5.       iCur = ColumnHeader.Index - 1
  6.       If iCur = iLast Then .SortOrder = IIf(.SortOrder = 1, 0, 1)
  7.      .SortKey = iCur
  8.       iLast = iCur
  9.      End With
  10.    End Sub
This works as in alist view as:
num
1
12
2
23
2111
3
356

What wrong with the code???
THanks in advance:
Dec 22 '07 #1
Share this Question
Share on Google+
6 Replies


debasisdas
Expert 5K+
P: 8,127
What is wrong with the output? It is showing as per code. Sorting is done as per ascending sort considering as character not numbers.
Dec 22 '07 #2

100+
P: 103
What is wrong with the output? It is showing as per code. Sorting is done as per ascending sort considering as character not numbers.
But I have to sort like
1
2
3
11
21
32
356
as if in the correct order, but the output is the one which is in #1, please go through.
Dec 22 '07 #3

debasisdas
Expert 5K+
P: 8,127
it is sorting considering the items as character strings not numbers
Dec 22 '07 #4

100+
P: 103
it is sorting considering the items as character strings not numbers
Please help me to sort a list view including characters and numbers
Thanks in advance
Dec 27 '07 #5

debasisdas
Expert 5K+
P: 8,127
If a list view is sorted including characters and numbers ,that will be sorted as per character only. So it will consider numbers as characters also.
Dec 27 '07 #6

Expert 5K+
P: 8,434
The basic problem here is that the sort functionality of the Listview control is treating the values as strings. If you consider the values to be strings, "2" is certainly a higher value than "15", simply because the "2" (ASCII code 50) is higher than the "1" (ASCII code 49). I imagine things might be more complex if dealing with Unicode.

This confusion between numbers and strings has been around for many years, and there are various ways in which one might work around it. These include (but are not limited to):
  • If possible, use your own code to do the sort, converting all values either to numbers, or two strings of the same length (such as "00015" and "00002"). I don't know whether the Listview control provides any way to customise the sorting, or not.
  • If the Listview control supports it, store the values in it as numbers rather than strings.
  • Add a hidden column and sort on that. In other words, if supported, add another column which is not possible to the user, and copy the values from the problematic column in a "sortable" format. For example, with leading zeroes as described in the first bullet point.
  • Sort the data before placing it in the listview. If it's coming form a database, this might mean including an ORDER BY clause in your SQL. If not, you might need to sort an array first, or simply change the order in which you add them to the ListView.
.
Dec 28 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.