Sort Numbers in order in listview?
Hi guys, I have a list view which has numbers in subitems(3)
now how can I make it so when Command1 is pressed it will sort by highest Value first. at the moment when I sort it it sorts by the first number not the entire value.
For example
99000
9
9
9
8
8
8
80000
I'd like it in order like
99000
80000
9
9
9
8
8
8
Re: Sort Numbers in order in listview?
Re: Sort Numbers in order in listview?
because what's in a list view is just text... it doesn't know that what's in the cells is numerical, so it's doing a string sort ... there are two ways to overcome this... 1 - you handle the sort, sort your data accordingly, clear the list view and re-load it with everything in the "right" order. 2 - you pad your shorter numbers with 0's .... so it becomes like this:
99000
80000
00009
00009
00009
00008
00008
00008
-tg
Re: Sort Numbers in order in listview?
SubItems are a Variant/String, so the sorting goes along those lines.
To sort by value you have two Options (As far as i know)
1) Transfer the ListView-Entries to a corresponding Array and pass it to a sorting function (e.g. Quicksort) incl. forcechanging the sorting criteria to Numericals, then repopulate your ListView
2) Determine the length of the longest "string" in SubItems(3) and add leading zeros to the "shorter" entries.
Maybe someone else has a better idea.
Re: Sort Numbers in order in listview?
Hi guys, thanks for the replies,
Do you have any example codes?
Regards,
Jamie
Re: Sort Numbers in order in listview?
Re: Sort Numbers in order in listview?
Now, if you want to see the data as it is
What I do is add a new column to the ListView control, set width = 0
Store the formatted data in that 'hidden' column, formatted as post #3 says
and define that column as the sorted one
Re: Sort Numbers in order in listview?
you can try this.. listview1.listitems.add=format(text1.text,"0000000000")..all must have the same digits to get it sorted
Re: Sort Numbers in order in listview?
Re: Sort Numbers in order in listview?
Zvoni, either example codes would be handy, this is killing me!
Madnessman, that code is used for dates, as appose to numbers / values.
Re: Sort Numbers in order in listview?
Jamie,
i'm going for the second option.
What's your source for the ListView? An Array? a recordset?
EDIT: I'm retracting. I'm going for the first option
Re: Sort Numbers in order in listview?
The simplest way is to right align the Column, add the data and then sort. Vis:
Code:
Option Explicit
Private Sub Form_Load()
Dim li As ListItem
Dim intI As Integer
lv.View = lvwReport
lv.ColumnHeaders.Add , , "Col1"
lv.ColumnHeaders.Add , , "Col2", , lvwColumnRight
Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
li.SubItems(1) = "8"
intI = intI + 1
Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
li.SubItems(1) = "80000"
intI = intI + 1
Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
li.SubItems(1) = "5"
intI = intI + 1
Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
li.SubItems(1) = "2"
intI = intI + 1
Set li = lv.ListItems.Add(, , "Row " & CStr(intI))
li.SubItems(1) = "90000"
intI = intI + 1
End Sub
Private Sub lv_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
lv.SortKey = ColumnHeader.Index - 1
lv.SortOrder = lvwDescending
lv.Sorted = True
End Sub
EDIT: It wont work if you're trying to sort on the very first column, as, for reasons I don't understand, the first column of a ListView must be left aligned.
EDIT2: If you still want the column to be left aligned then you can just use
Code:
Private Sub lv_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
lv.ColumnHeaders.Item(2).Alignment = lvwColumnRight
lv.SortKey = ColumnHeader.Index - 1
lv.SortOrder = lvwDescending
lv.Sorted = True
lv.ColumnHeaders.Item(2).Alignment = lvwColumnLeft
End Sub
1 Attachment(s)
Re: Sort Numbers in order in listview?