History of Idiom 100 > diff from v16 to v17
Edit summary for version 17 by :
Restored version 15
Restored version 15
↷
Version 16
2016-02-18, 16:58:02
Version 17
2016-02-18, 18:53:27
Idiom #100 Sort by a comparator
Sort elements of array-like collection items, using a comparator c.
Idiom #100 Sort by a comparator
Sort elements of array-like collection items, using a comparator c.
Code
type Integer_Comparator is not null access function (Left, Right : Integer) return Boolean;
package Integer_Vectors is new Vectors (Positive, Integer);
use Integer_Vectors;
procedure Sort_Using_Comparator (V : in out Vector; C : Integer_Comparator) is
package Vector_Sorting is new Generic_Sorting (C.all);
use Vector_Sorting;
begin
Sort (V);
end Sort_Using_Comparator;
Code
type ItemCSorter []Item
func (this ItemCSorter) Len() int { return len(this) }
func (this ItemCSorter) Less(i, j int) bool { return c(this[i], this[j]) }
func (this ItemCSorter) Swap(i, j int) { this[i], this[j] = this[j], this[i] }
func sortItems(items []Item) {
sorter := ItemCSorter(items)
sort.Sort(sorter)
}
Code
type ItemCSorter []Item
func (this ItemCSorter) Len() int { return len(this) }
func (this ItemCSorter) Less(i, j int) bool { return c(this[i], this[j]) }
func (this ItemCSorter) Swap(i, j int) { this[i], this[j] = this[j], this[i] }
func sortItems(items []Item) {
sorter := ItemCSorter(items)
sort.Sort(sorter)
}
Comments bubble
Comments bubble