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