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.
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
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.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
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
JG
... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...
What's your source for the ListView? An Array? a recordset?
EDIT: I'm retracting. I'm going for the first option
Last edited by Zvoni; Oct 9th, 2012 at 10:11 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
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
Last edited by Doogle; Oct 10th, 2012 at 12:15 AM.