Newsgroup: it.comp.lang.visual-basic
Titolo: listview with sortkey + date problem

LISTVIEW SORTING:
I know how to set the sortkey to the column i want to sort on and all
that, but it's only sorting on the first number. 
for instance:  11 comes before 2 with sortorder = 0 (Ascending)
SO: How do I get it to sort on the whole number?
DATE :
If i want to add a week to Date i can easily do a : Date + 7 
but to add a year or a month is more difficult.
I use Mid$ like this to add a year: 
Dim Dat as date
Dat = 14.12.2000
Dat = Mid$(Dat, 1, 6) & (Mid$(Dat, 7, 4) + 1)
BUT if a date variable is set in a Calendar in a prog the it could be
12.14.2000 or 2000.12.14 depending how the date format i set in the
OS? And I don't what to "format$" it.
SO: How can a add a year/month to a date variable without using Mid$
and format$
Hope someone can help me here:-)
--
Rikard Bø

Look on msdn for this article:
HOWTO: Sort a ListView Control by Date 
Q170884
http://support.microsoft.com/support/kb/articles/Q170/8/84.asp
hth, hal



yes, it sorts by date, but it seems like it's just changing the name
of the items in the row. if i have 3 rows: 
row names "row1" ( index = 1 )
row names "row2" ( index = 2 )
row names "row3" ( index = 3 )
because of the subitem date it gets sorted by that module like: 
row names "row3" ( index = 3 )
row names "row2" ( index = 2 )
row names "row1" ( index = 1 )
if i select the first row named "row3" and delete it ( controlled on
selecteditem.index)..... then the row named "row1" gets removed...
so if i select the first row, now called "row3" it has the index 3.
so ListView1.ListItems.Remove (lstEvent.SelectedItem.Index)  removes
row number 3, not the row with index 3!
the regular sort in vb does not bring the index along with the item
(row) when sorting..... but that's what this module does, and that
causes the problem i tried to explain:-) hope you understood it.... if
you did, do you know how to fix this?

to avoid this ?
A good question. The VB4.0 help file is clearly wrong about this, since
it says about the ListView.SortOrder property:
Constant Value Description
lvwAscending 0 (Default) Ascending order. Sorts from the beginning
of the alphabet (A-Z) the earliest date, or the
lowest number.
lvwDescending 1 Descending order. Sorts from the end of the
alphabet (Z-A), the latest date, or the highest
number.
In fact, the sort order and .Sorted property do none of this. The
ListView only sorts in lexical (alphabet) order. Numbers will notsort
correctly if they have different numbers of digits:
1
11
2
23
I have reported this bug/inconsistency to Micro$oft, but, of course, I
have not seen or heard any reply because they don't specifically respond
to bug reports.
All you need to do is left-justify the numbers to the same length using
blanks. Then the alpha sort has the same effect as the numeric sort.
-----------------------------------------------------------------
DATES:
-----------------------------------------------------------------
Here is a quick and simple solution that I have used. Basically, add a
temporary column to the list view, put data in a format that will sort
correctly, sort on that column, then get rid of the temporary column. By
using LockWindowUpdate() before and after the operation, you see nothing
of this on the screen, it is quick, and takes only about 8 lines of
code. To sort dates, convert your date column into a number of seconds
(UNIX epoch) and sort on that. This method has the benefit of being
independent of the date format used by the rest of your program. Given a
listview named lvMine with integers in its .subitems(0):
------------------------>
dim liX as ListItem
dim ciX as ColumnHeader
dim i%, sortcol%
LockWindowUpdate lvMine.hWnd
Set ciX = lvMine.ColumnHeaders.Add ' add the temporary sort column
sortcol% = ciX.Index - 1
' convert all items in col 0 into sortable values in sortcol%
For i% = 1 to lvMine.ListItems.count
Set liX = lvMine.ListItems(i%)
' and set the temp column value to a converted, sortable value
liX.SubItems(sortcol%) = DateDiff( "s", _
"00:00:00 1 Jan 1970", liX.SubItems(0))
Next i%
lvMine.SortKey = sortcol% ' sort on the temp column
lst.ColumnHeaders.Remove ciX.Index ' then get rid of it.
LockWindowUpdate 0&
<------------------------
And that does it. You don't need the LockWindowUpdate, but it makes it
nice. The declaration is:
Declare Function LockWindowUpdate Lib "user32" _
(ByVal hwnd as Long) as Long



this sorts a column of seconds and then hides it. but it's not solving
the problem. it's converting the date so it can be sortet, BUT it's
still just sorting on the first number... 
1
2
3809032409523095823052034523094532
4
50000
90
i could add 0's or " "'s but i don't want to do that. 
SO: How can I sort a column? date or not!

 SO: How can I sort a column? date or not!
rikard
sorry, as for digits, i only found one way. right-align the (sub) item,
then pad the digit with spaces at his left. something like the code here
below. if you discover anything else, please let me know, i'm interested
too.
    tmpFile = Dir(MyPath & "*.*", nShow) 'shr too
    While tmpFile <> ""
        tmpCnt = tmpCnt + 1
        tmpFileFull = MyPath & tmpFile
        Set itmX = LV.ListItems.Add()
        itmX.Text = tmpFile
        tmpExt = IIf(InStr(tmpFile, ".") <> 0, ParseFExt(tmpFile), "")
        itmX.SubItems(1) = tmpExt
        lSize = Len(Format(FileLen(tmpFileFull), "###,###,###,###"))
        itmX.SubItems(2) = Space(15 - lSize) & _
            Format(FileLen(tmpFileFull), "###,###,###,###")
        itmX.SubItems(3) = Format(FileDateTime(tmpFileFull), "yyyy/mm/dd
hh:mm:ss")
        totSize = totSize + FileLen(tmpFileFull)
        tmpFile = Dir
    Wend