Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Apr 6, 2000

Sort Non-String Items in a ListView

Sorting ListView columns with numeric data can be a real pain. Nonstring sorting is possible with callbacks using custom comparison functions, but this method's drawback is that the synchronization between the display and ListItems collection is lost. It's easier and more reliable to simply provide sortable data. Normally a list with the values 1, 2, 3, 4, 10, and 20 sorts as 1, 10, 2, 20, 3, and 4—that's not very useful. A simple workaround is to "left-pad" the numeric data with spaces before setting the text value. Assume that you load a listview from a recordset with last name, first name, and salary:
 
Const MAX_WIDTH = 15
Dim szSpaces As String
Dim rs As Recordset
szSpaces = Space$(MAX_WIDTH)
Do Until rs.EOF
	With ListView1.ListItems.Add(, , rs("LastName"))
	.SubItems(1) = rs("FirstName")
	.SubItems(2) = Right$(szSpaces & _
		rs("Salary"), MAX_WIDTH)
	End With
	rs.MoveNext
Loop
Now setting the ListView's Sorted property sorts the Salary column in correct numerical order.
Amir Liberman
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date