Page 1 sur 1
[R] Simple tri
Posté : lun. 20 janv. 2025 10:34
par Yle
Bonjour
Je cherche pourquoi le tri ASCENDANT ou DESCENDANT ne se fait pas. Je dois certainement faire une GROSSE erreur que je ne vois pas.
Ci-joint mon très court programme
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>
$i1 = InputBox("Controle scrores","Entrer autant de scores (dans n'importe quel ordre) que vs voulez séparés par une virgule","52,-100,-55,66"," ",500,200)
$repart = StringSplit($i1,",")
_ArraySort($repart,0,1) ; Tri à partir 2eme ligne
_ArrayDisplay($repart)
Exit
D'avance merci
Re: [..] Simple tri
Posté : lun. 20 janv. 2025 18:04
par Nine
Le problème c'est que StringSplit convertit automatiquement tes nombres en String (c'est pourquoi on l'appelle StringSplit). Tu dois convertir chacune des valeurs de ton array $repart en nombre avec Number(). Après le _ArraySort va fonctionner normalement.
Re: [..] Simple tri
Posté : mar. 21 janv. 2025 17:40
par sylvanie
Bonjour
+1 sur la conversion, par contre je passerai par une listeview pour y assigner une callback de tri (j'ai repris l'exemple de l'aide de GUICtrlRegisterListViewSort), car je n'arrive pas à le faire avec _ArrayDisplay
; sorting 3 column's different#include <GUIConstantsEx.au3>#include <ListViewConstants.au3>Global $g_iCurCol = -1Global $g_iSortDir = 1Global $g_bSet = FalseGlobal $g_iCol = -1#include <MsgBoxConstants.au3>#include <StringConstants.au3>#include <Array.au3>$i1 = InputBox("Controle scrores","Entrer autant de scores (dans n'importe quel ordre) que vs voulez séparés par une virgule","52,-100,-55,66"," ",500,200)$repart = StringSplit($i1,",",2)For $ind= 0 To UBound($repart)-1 $repart[$ind]=int($repart[$ind])Next_ArraySort($repart)Example
()Func Example
() GUICreate("Test", 300, 200) Local $idLv = GUICtrlCreateListView("Scores", 10, 10, 280, 180) GUICtrlRegisterListViewSort(-1, "LVSort") ; Register the function "LVSort" for the sorting callback For $ind= 0 To UBound($repart)-1 GUICtrlCreateListViewItem($repart[$ind], $idLv) Next GUISetState(@SW_SHOW) Local $idMsg ; Loop until the user exits. While 1 $idMsg = GUIGetMsg() Switch $idMsg Case $GUI_EVENT_CLOSE ExitLoop Case $idLv $g_bSet = False $g_iCurCol = $g_iCol GUICtrlSendMsg($idLv, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($idLv), 0) EndSwitch WEnd GUIDelete()EndFunc ;==>Example; Our sorting callback funtionFunc LVSort
($hWnd, $nItem1, $nItem2, $iColumn) Local $sVal1, $sVal2, $iResult ; Switch the sorting direction If $iColumn = $g_iCurCol Then If Not $g_bSet Then $g_iSortDir = $g_iSortDir * -1 $g_bSet = True EndIf Else $g_iSortDir = 1 EndIf $g_iCol = $iColumn $sVal1 = Int(GetSubItemText
($hWnd, $nItem1, $iColumn)) $sVal2 = Int(GetSubItemText
($hWnd, $nItem2, $iColumn)) ; If it is the 3rd colum (column starts with 0) then compare the dates $iResult = 0 ; No change of item1 and item2 positions If $sVal1 < $sVal2 Then $iResult = -1 ; Put item2 before item1 ElseIf $sVal1 > $sVal2 Then $iResult = 1 ; Put item2 behind item1 EndIf $iResult = $iResult * $g_iSortDir Return $iResultEndFunc ;==>LVSort; Retrieve the text of a listview item in a specified columnFunc GetSubItemText
($idCtrl, $idItem, $iColumn) Local $tLvfi = DllStructCreate("uint;ptr;int;int[2];int") DllStructSetData($tLvfi, 1, $LVFI_PARAM) DllStructSetData($tLvfi, 3, $idItem) Local $tBuffer = DllStructCreate("char[260]") Local $nIndex = GUICtrlSendMsg($idCtrl, $LVM_FINDITEM, -1, DllStructGetPtr($tLvfi)); Local $tLvi = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int") DllStructSetData($tLvi, 1, $LVIF_TEXT) DllStructSetData($tLvi, 2, $nIndex) DllStructSetData($tLvi, 3, $iColumn) DllStructSetData($tLvi, 6, DllStructGetPtr($tBuffer)) DllStructSetData($tLvi, 7, 260) GUICtrlSendMsg($idCtrl, $LVM_GETITEMA, 0, DllStructGetPtr($tLvi)); Local $sItemText = DllStructGetData($tBuffer, 1) Return $sItemTextEndFunc ;==>GetSubItemText
Re: [..] Simple tri
Posté : mar. 21 janv. 2025 17:55
par Nine
Il ne faut pas utiliser Int() mais Number(), car sinon tu vas casser tous tes floats.
Re: [..] Simple tri
Posté : mer. 22 janv. 2025 10:28
par Yle
Merci pour la solutiok