proxy.golang.org : github.com/Merovius/mtp-esort
Package esort provides mechanisms for sorting user-defined types according to compound criteria extensibly. It is mutually compatible with package sort from the standard library. The package name comes from the combination of the words "extensible" and "sort" and shortened as "esort." Consider a case of sorting a user-defined type of a Person: Suppose GivenName is to be sorted in descending order and ties of the GivenName by ID in ascending order: The data can then be sorted: The same mechanism can be used directly with generics: The methods prefixed with By copy the current sorting rule set and add a new instruction to the copy. Each rule set is evaluated according to the order in which the instructions were added to the Sorter, so earlier sorting rules carry higher sorting weight than the previous. A Sorter without rules is invalid and panics upon use. Due to the copy on write semantics of the instructions API, rule sets can be templatized: Prefer using the simple, scalar By methods for comparing individual fields versus encoding their logic in the Sorter.ByFunc method. These individual methods are implemented efficiently. Prefer: Over: Prefer defining the sorters as top-level variables if they have a static sorting basis. They will allocate zero memory during later program runtime — provided of course no user-defined functions registered with ByFunc do. The ByFunc method enables types to be sorted by user-defined functions. The ByFunc method follows the same logic as the other instructions; it is appended to the rule set and evaluated accordingly. If the function provided to ByFunc compares only one sorting basis, it may be implemented in a naive fashion whereby it only compares one side of the basis. Consider a data-transmission object (DTO) The function lessDTO needn't consider the inverse case: The Sorter checks inverse cases automatically. The examples above are implemented using anonymous functions, similar to this: You are free to use free-standing, top-level functions: And method expressions on the types, though consider whether getters are even appropriate for the API first: A hand-implemented sort.Interface.Less function readily beats a Sorter. As implemented today, a Sorter with a simple double-compound sort criteria takes approximately 5-7x the time of a hand-written Less.
Registry
-
Source
- Documentation
- JSON
purl: pkg:golang/github.com/%21merovius/mtp-esort
License: Apache-2.0
Latest release: about 1 year ago
Namespace: github.com/Merovius
Stars: 4 on GitHub
Forks: 1 on GitHub
See more repository details: repos.ecosyste.ms
Last synced: about 1 month ago