Custom Comparison Function for Table.Sort()
to make Table Sorting case-insensitive
to make Table Sorting case-insensitive
Rationale:
When attempting to sort table values alphabetically, the default outcome of the Table.Sort() command on the following table:
Code:
tList = {"Dave", "dave", "mary", "Mary", "Adam", "adam"}; Table.Sort(tList, nil); Debug.Clear(); Debug.ShowWindow(true); for key, value in pairs (tList) do Debug.Print(value.."\r\n"); end
Code:
Adam Dave Mary adam dave mary
.
Eg.
By ASCII definition, the lower case "a" character is decimal 97.
By ASCII definition, the Upper Case "A" character is decimal 65.
.
So, by default Upper Case will always be displayed first.
.
To get the Table.Sort() command to override its default output and instead, display output in a ‘more-ideal’ case-insensitive manner such as this:
Code:
Adam adam Dave dave Mary mary
.
To this end (and to save others some needless head banging), three examples of custom comparison functions are demonstrated in this .Apz example. The 1st example may be considered as the simplest solution. But the 3rd and final example may be considered as being the most thorough and robust solution.
.
Enjoy. And may the coding gods be with you.
- BioHazard
Comment