# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Vb6 - Sort Listview By Dates/numbers/text

## kleinma

the title explains it all

as most of you know, you can only do text sorts on listview in report style, which is a big pain. MS provides an API method in the KB on how to sort by dates, but it is cumbersome, and it doesn't order the listitems, so after you use it, you can't do a for each listitem in listitems loop because the order of the listitems collection doesn't change. this code fixes all that, and doesn't even use any API

there is an example attached that will show you how it works.

----------


## batori

here is another simple example :



```
Private Sub ListView2_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)

    ListView2.SortKey = ColumnHeader.Index - 1

    If ListView2.SortKey = 1 Then ListView2.SortKey = 3 ' **** This column changes the key
    ListView2.SortOrder = (ListView2.SortOrder - 1) * -1
    ListView2.Sorted = True

End Sub
```

----------


## JamieWarren09

Hi, Fantastic code as it's doing what i want to do kind of. Any idea how I can make it sort numbers correctly.

For example i have 

1000
2000
9,900
22,000
82,000


and when I sort it it only sorts by the first number.  I was hoping it would sort by highest amount as apose to the first number?

I basically want to sort it via highest value / lowest value. 


any ideas?


Jamie.

----------


## VBClassicRocks

One way is to make all the numbers have the same length by
prepending spaces.  Formatting with the '@' character
right justifies the number with spaces.



```
Private Sub Form_Load()
 Dim n As String
 Dim i As Long
 'change this constant, depending on expected number length
 ' or calculate it
 Const MaxLen As Long = 6
 For i = 1 To 10
  n = Format$(i, String$(MaxLen, "@"))
  Debug.Print n, Len(n)
 Next
End Sub
```

----------


## JamieWarren09

Hi mate, I'm really confused as to where I should put that code.

Basically at the moment in the listview when I sort it, it will go by the first number not the whole figure...

Any ideas?

----------


## VBClassicRocks

What that code shows is making the numbers the same length
with prepended spaces so the numbers will sort properly.

The code below shows using numbers in the 2nd column (SubItem(1))



```
Option Explicit
Private Sub Form_Load()
 Dim i As Long
 Dim Num As String
 Const MaxLen As Long = 8 'change as necessary for the
                          'expected length of the numbers
 Randomize
 ListView1.Sorted = False
 For i = 1 To 100
  With ListView1.ListItems.Add
   .Text = "Item " & i
   Num = Format$(Int(Rnd * i * 1000) + 1, String$(MaxLen, "@"))
   'you can see how this works by using '0' vice '@' in the above line
   .SubItems(1) = Num
  End With
 Next
 ListView1.SortKey = 1 'subitem(1)
 ListView1.Sorted = True
End Sub

Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
 With ListView1
  .SortKey = ColumnHeader.Index - 1
  .SortOrder = .SortOrder Xor 1 'toggle ascending/descending
 End With
End Sub
```

----------

