Champak Posted June 12, 2023 Posted June 12, 2023 (edited) How can I "properly" sort the following array? I'm sure it's been done already, but I don't know the proper search term to narrow down what I'm looking for. Edited June 12, 2023 by Champak Title
Solution AspirinJunkie Posted June 12, 2023 Solution Posted June 12, 2023 The term you are looking for is "natural sort". There are several functions here in the forum for this. With >>this UDF<< your example would be solved like this: #include "ArrayPlus.au3" Global $aArray[11] = [10, "InventorySave_1.lvs", "InventorySave_10.lvs", "InventorySave_2.lvs", "InventorySave_3.lvs", "InventorySave_4.lvs", "InventorySave_5.lvs", "InventorySave_6.lvs", "InventorySave_7.lvs", "InventorySave_8.lvs", "InventorySave_9.lvs"] ; sort with natural comparison function _ArraySortFlexible($aArray, __ap_cb_comp_Natural, 1) ; display the array _ArrayDisplay($aArray) Champak 1
ioa747 Posted June 12, 2023 Posted June 12, 2023 just in case #include <Array.au3> Local $aArray[101] $aArray[0] = 100 For $i = 1 To $aArray[0] $aArray[$i] = "InventorySave_" & $i & ".lvs" Next ;~ _ArrayShuffle($aArray, 1) _ArraySort($aArray, 0, 1, 0, 1) _ArrayDisplay($aArray, "BEFORE") _SortInventoryArray($aArray) _ArrayDisplay($aArray, "AFTER") Func _SortInventoryArray(ByRef $Array) _ArrayColInsert($Array, 1) ; Now a 2D array For $i = 1 To $Array[0][0] $Array[$i][1] = StringFormat("%010s", StringTrimLeft($Array[$i][0], 14)) Next _ArraySort($Array, 0, 1, 0, 1) ;~ _ArrayDisplay($Array, "2D array") _ArrayColDelete($Array, 1) EndFunc ;==>_SortInventoryArray more: forum/topic/209849 fraizor 1 I know that I know nothing
mikell Posted June 12, 2023 Posted June 12, 2023 An other workaround ... #include "Array.au3" Global $aArray[11] = [10, "InventorySave_1.lvs", "InventorySave_2.lvs", "InventorySave_3.lvs", "InventorySave_4.lvs", "InventorySave_10.lvs", "InventorySave_5.lvs", "InventorySave_6.lvs", "InventorySave_7.lvs", "InventorySave_8.lvs", "InventorySave_9.lvs"] For $i = 1 to $aArray[0] $aArray[$i] = Execute("'" & StringRegExpReplace($aArray[$i], "\d+", "' & StringFormat('%03s', '$0') & '") & "'") Next _ArraySort($aArray) ;_ArrayDisplay($aArray) For $i = 1 to $aArray[0] $aArray[$i] = StringRegExpReplace($aArray[$i], '\D\K0+', "") Next _ArrayDisplay($aArray)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now