History of Idiom 100 > diff from v15 to v16
Edit summary for version 16 by :
New Haskell implementation by user [JH]
New Haskell implementation by user [JH]
↷
Version 15
2016-02-17, 17:33:51
Version 16
2016-02-18, 16:58:02
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