Quantcast
Channel: Indigo Rose Software Forums - Forums
Viewing all articles
Browse latest Browse all 2105

Custom Comparison Function for Table.Sort()

$
0
0
Custom Comparison Function for Table.Sort()
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
...will produce this ‘less-than-ideal’ result:
Code:
Adam
Dave
Mary
adam
dave
mary
This outcome is produced by default because the ASCII definition for "lower case" is greater than that of "Upper Case"
.
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
...it's first necessary to add a custom comparison function to the Table.Sort() command’s parameters.
.
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

Viewing all articles
Browse latest Browse all 2105

Trending Articles