Module sort
This module contains a collection of comparison functions (or factories for comparison functions) for table.sort.
Functions
natsort_cmp (cache) | Generates a natural sorting comparison function for table.sort. |
Functions
- natsort_cmp (cache)
-
Generates a natural sorting comparison function for table.sort.
Parameters:
- cache Optional, hashmap used to cache the processed strings to speed up sorting
Returns:
- The cmp function to feed to table.sort
- The cache used (same object as the passed one, if any; will be created if not)
Usage:
-- t is an array of strings, we don't want to keep the cache around table.sort(t, sort.natsort_cmp()) -- t is an array of arrays, we want to sort the strings in the "text" field of the inner arrays, and we want to keep the cache around. local cmp, cache cmp, cache = sort.natsort_cmp(cache) table.sort(t, function(a, b) return cmp(a.text, b.text) end)