Search code examples
autohotkeyalphabeticalalphabetalphabetical-sort

Alphabetizing a List


I'm looking for a way to truly alphabetize a list. Assuming it's a list of basic words, such as:
Black
Green
The Red
Blue
Waxy
Living
Porous
Solid
Liquid
Violet

Is there a way to modify this code to alphabetize the list where "The Red" comes before "Solid"? Here's what I have so far:

SaveVar=%ClipboardAll%
Clipboard=
Send ^c
ClipWait, 0.5
Sort clipboard, CL
;Process exceptions
Sort := RegExOmit (Sort, "The")
Send ^v
Sleep 100
Clipboard=%SaveVar%
SaveVar=
return

Solution

  • Write a custom comparison function that ignores the starting "The " substring.

    list = Black`nGreen`nThe Red`nBlue`nWaxy`nLiving`nPorous`nSolid`nLiquid`nViolet`nThe Azure
    
    Sort , list , F Compare
    MsgBox, %list%
    
    Compare( a , b )
    {
        arem := RegExReplace(a, "A)The " , "" )
        brem := RegExReplace(b, "A)The " , "" )
    
        return arem > brem ? 1 : arem < brem ? -1 : 0
    }
    

    Regular expressions are used to remove the substring "The " from the string and the result stored in a temporary string, which is then used for comparison.

    The substring must start at the beginning of the string, regex option A), and must include a space immediately after The.