Custom string sorter has performance issues.
Posted: 12 May 2023, 16:57
Today was apparently the day when I had enough of my computer telling me I can't spell, so I made a custom string sorter I made it in Python, since I'm not that good at ahk, and then attempted to port it. This is where it the troubles began. I finally got it to work, but it is not running smoothly. It's about three orders of magnitude slower than my Python code, so I suspect that something is getting lost in translation. The function:
It's just a function that sorts strings like in dictionaries/libraries (physical ones) and lets you decide which characters go in what order. Stupid example:
This might seem useless if you are from an English speaking country, but I'm guessing that Swedish is not the only language that gets mangled when trying to sort strings, so there might be some interest in a function like this, if it were a bit more efficient. I don't actually need the function, since I have a Python version that takes thousands more words before getting sluggish, but I'm very curious as to what went wrong when trying to port it. I know bubble sort is not a very fast algorithm, but I gave up on trying to write an implementation of quicksort after a while. Btw, the Ahk version is not 3 orders of magnitude slower than the Python quicksort one. I made a somewhat fair comparison and timed the Python version also using bubble sort. I'm just curious to find out where I went wrong, but if you see any usefulness in the function, feel free to improve it.
If you are from an English speaking country and wanna relate to my dilemma, you can imagine most of your apps and algorithms telling you that "e" comes before "a" in the alphabet
Code: Select all
CustomSort(ListofStrings, MyOrder){
; Create a copy of the list so it doesn't get edited
ListofStringsCopy := ListofStrings.Clone()
; Create a dictionary with scores for each character in MyOrder
scores := {}
For index, char in MyOrder
scores[char] := index
; Sort the array using bubble sort
n := ListofStrings.Length()
While (n > 0) {
newn := 0
Loop, % n-1 {
i := A_Index
s1 := ListofStringsCopy[i]
s2 := ListofStringsCopy[i+1]
len_s1 := StrLen(s1)
len_s2 := StrLen(s2)
len_min := len_s1 < len_s2 ? len_s1 : len_s2
temp := 0
Loop, % len_min {
c1 := SubStr(s1, A_Index, 1)
c2 := SubStr(s2, A_Index, 1)
If (!scores.HasKey(c1) && !scores.HasKey(c2))
Continue
If (!scores.HasKey(c1)) {
temp := 1
Break
}
If (!scores.HasKey(c2)) {
temp := -1
Break
}
If (scores[c1] < scores[c2]) {
temp := -1
Break
}
If (scores[c1] > scores[c2]) {
temp := 1
Break
}
}
If (!temp)
temp := len_s1 - len_s2
If (temp > 0) {
; Swap items
ListofStringsCopy[i] := s2
ListofStringsCopy[i+1] := s1
newn := i + 1
}
}
n := newn
}
Return ListofStringsCopy
}
Code: Select all
MyOrder := ["@", "B", "C"]
ListofStrings := ["B@c", "Bb", "b@BC"]
CustomSort(ListofStrings, MyOrder)
>> ["b@BC", "B@c", "Bb"]
If you are from an English speaking country and wanna relate to my dilemma, you can imagine most of your apps and algorithms telling you that "e" comes before "a" in the alphabet