之前一直在为这个问题头大,搜索了一下帮助文档,看到_GUICtrlListView_SimpleSort函数有这个功能。
虽然 ...
nmgwddj 发表于 2011-9-26 02:48
你这是属于典型的眼见了心要烦,象$LVS_SORTASCENDING那样给你个黑箱,你就不焦虑了,其实其内部实现,还是一样原理:每插入一次,就得调用一下排序。
还是来个任何语言都会教授的二分法排序来玩一下吧
#include <array.au3>
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Local $hListView, $aRet, $index
GUICreate("ListView", 600, 400)
$hListView = GUICtrlCreateListView("Auto_Sorted", 20, 20, 550, 350)
GUISetState(@SW_SHOW)
$aRet = _Random_Array()
_GUICtrlListView_BeginUpdate($hListView)
For $i = 1 To $aRet[0]
$index = _GetSortedIndex($hListView, $aRet[$i])
_GUICtrlListView_InsertItem($hListView, $aRet[$i], $index)
Next
_GUICtrlListView_SetColumnWidth($hListView, 0, $LVSCW_AUTOSIZE)
_GUICtrlListView_EndUpdate($hListView)
Do
Until Sleep(10) * GUIGetMsg() == -3
Func _GetSortedIndex($hListView, $item)
Local $iCount = _GUICtrlListView_GetItemCount($hListView)
Local $iStart = 0, $iEnd = $iCount, $iMid = Int($iEnd/2)
If $iCount == 0 Then Return
While ($iEnd - $iStart) > 10
If StringCompare($item, _GUICtrlListView_GetItemText($hListView, $iMid)) <= 0 Then
$iEnd = $iMid
$iMid -= Int(($iEnd - $iStart) / 2)
Else
$iStart = $iMid
$iMid += Int(($iEnd - $iStart) / 2)
EndIf
WEnd
For $i = $iStart To $iEnd
If StringCompare($item, _GUICtrlListView_GetItemText($hListView, $i)) <= 0 Then Return $i
Next
EndFunc
Func _Random_Array()
Local $array[201] = [200], $string = ''
For $i = 1 To 200
$string = ''
For $j = 1 To 10
$string &= Chr(Random(97, 122))
Next
$array[$i] = $string
Next
Return $array
EndFunc
|