经测试,楼上的代码有时并不能正确排序,修改如下:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListBoxConstants.au3>
#include <File.au3>
#include <array.au3>;注意增加了数组函数
;#include <ListviewConstants.au3>
;#Include <Constants.au3>
;Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
;Global $Input2 ,$form2
Local $width = 800
Local $height = 600
$Form1 = GUICreate("Form1", $width, $height, (@DesktopWidth - $width) / 2, (@DesktopHeight - $height) / 2)
$Label1 = GUICtrlCreateLabel("下边的框是个输入框GUICtrlCreateInput创建的", 100, 480, 150, 30)
$Input3 = GUICtrlCreateInput("在此粘贴要排序的数据", 104, 510, 260, 21)
$Button3 = GUICtrlCreateButton("重新排序", 8, 540, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $GUI_EVENT_PRIMARYDOWN
Case $Button3
Local $s, $sArray, $i
$s = StringStripWS(GUICtrlRead($Input3), 1 + 2 + 4)
If Not StringRegExp($s, '^(\d+\s)+\d+$', 0) Then
MsgBox(64, "输入有误", "你输入的数据格式不正确!")
ContinueLoop
EndIf
$sArray = StringSplit($s, " ")
GUICtrlSetData($Input3, _NumSort())
EndSwitch
WEnd
Func _NumSort() ;原_arraysort()函数有一定局限,为使程序简洁将此段代码独立出来,不具备通用性
_ArrayDelete($sArray, 0)
For $i = 0 To UBound($sArray) - 1
$sArray[$i] = StringRegExpReplace($sArray[$i], '^0*', '');去掉数字前面的0
$sArray[$i] = Number($sArray[$i]);转换为数字格式
Next
_ArraySort($sArray, 0, 2)
;~ _ArrayDisplay($sArray)
For $j = 0 To UBound($sArray) - 1
If StringLen($sArray[$j]) = 1 Then
$sArray[$j] = StringFormat('%02d', $sArray[$j]);为<10的数加上前导0
EndIf
Next
Return _ArrayToString($sArray, ' ')
EndFunc ;==>_NumSort
|