Jump to content

sorting list view


Recommended Posts

Hi!

Had this annoying prob for a long while now....

$invoicelist = GUICtrlCreateListView ("Invoice #  |Date      |Firstname     |Surname        |Company        |Building       |Address1       |Address2       |Postcode         |Database ref#",0,0,600,350,bitor($LVS_SORTDESCENDING,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT,$LVS_SINGLESEL))

When I add invoices to the list, they are sorted, like so

999

998

997

996

Great!

But since I hit invoice 1000 I get this

999

998

997

996

1004

1003

1002

1001

I can see what its doing but the only way I have found so far around this problem is to add all the invoice to an array first, sort the array then add them to the list, but the trouble is they get added to the list ad - hoc (the list updates in real time) and so the new ones that get added then arent sorted properly :)

Any ideas?

Link to comment
Share on other sites

By the way, I call this function with this command to keep the list sorted when new items are added. This is the prob I think but not sure where to fix it :)

GUICtrlRegisterListViewSort(-1, "LVSort")

Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort
   ; Switch the sorting direction
    If $nColumn = $nCurCol Then
        If Not $bSet Then
            $nSortDir = $nSortDir * -1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
    EndIf
    $nCol = $nColumn
       
    $val1   = GetSubItemText($lvname, $nItem1, $nColumn)
    $val2   = GetSubItemText($lvname, $nItem2, $nColumn)
   
   ; If it is the 3rd colum (column starts with 0) then compare the dates
    If $nColumn = 2 Then
        $val1 = StringRight($val1, 4) & StringMid($val1, 4, 2) & StringLeft($val1, 2)
        $val2 = StringRight($val2, 4) & StringMid($val2, 4, 2) & StringLeft($val2, 2)
    EndIf
       
    $nResult = 0       ; No change of item1 and item2 positions
   
    If $val1 < $val2 Then
        $nResult = -1  ; Put item2 before item1
    ElseIf  $val1 > $val2 Then
        $nResult = 1   ; Put item2 behind item1
    EndIf

    $nResult = $nResult * $nSortDir
   
    Return $nResult
EndFunc
Link to comment
Share on other sites

Any ideas?

Use StringFormat() to make them all the same number of digits with leading zeros:

$n = "989"
$n = StringFormat("%06u", $n)
MsgBox(64, "Demo", "Like this:  " & $n)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

The guilist strips off the leading 0 as well !!!

That should only happen if you are passing it an integer variable instead of a string. The following two variables should be treated differently:

$Var1 = 000989
$Var2 = "000989"oÝ÷ Ù8^JÚâh®f­².ÖÞ+⨹«-®)àZ)íz«nër¢ç(ºWfjG¬º·°Øl¢g­)à)¶¬jëh×6#include <date.au3>

$n = "989"
$Date = _Now()
$InvNum = "123123"
$Cust = "Widgets Inc."
$RefDoc = "Purchase Order AB12CD34"
$Entry = StringFormat("%06u - %s - %012u - %s - %s", $n, $Date, $InvNum, $Cust, $RefDoc)
MsgBox(64, "Demo", "Like this:  " & $Entry)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi,

alternatuively, modify the Sort as you said originally;

Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort
    If $nColumn = $nCurCol Then; Switch the sorting direction
        If Not $bSet Then
            $nSortDir = $nSortDir * - 1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
    EndIf
    $nCol = $nColumn
    $val1 = GetSubItemText($lv, $nItem1, $nColumn)
    $val2 = GetSubItemText($lv, $nItem2, $nColumn)
    If (StringIsFloat($val1) Or StringIsInt($val1)) And (StringIsFloat($val2) Or StringIsInt($val2)) Then Return _Iif(Number($val1) < Number($val2), -1, _Iif(Number($val1) > Number($val2), 1, 0)) * $nSortDir
    Return _Iif($val1 < $val2, -1, _Iif($val1 > $val2, 1, 0)) * $nSortDir
EndFunc   ;==>LVSort
Best, Randall [Add back the date part too if needed for a specific column...]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...