Jump to content

array sort issue --- "natural sort"


Go to solution Solved by AspirinJunkie,

Recommended Posts

  • Solution

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)

 

Link to comment
Share on other sites

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

I know that I know nothing

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

  • Champak changed the title to array sort issue --- "natural sort"

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...