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 = 15Dim szSpaces As StringDim rs As RecordsetszSpaces = 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.MoveNextLoop

Now setting the ListView’s Sorted property sorts the Salary column in correct numerical order.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS