items.Sort(c);
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;
int c(const void *a,const void *b)
{
int x = *(const int *)a;
int y = *(const int *)b;
if (x < y) return -1;
if (x > y) return +1;
return 0;
}
int main(void)
{
int arr[]={1,6,3,7,2};
qsort(arr,sizeof(arr)/sizeof(*arr),sizeof(*arr),c);
return 0;
}
struct is_less {
bool operator () (int a, int b){
return a < b;
}
};
int main(){
std::vector<int> items = {1337, 666, -666, 0, 0, 666, -666};
std::sort(items.begin(), items.end(), is_less());
std::vector<int> expected = {-666, -666, 0, 0, 666, 666, 1337};
assert(items.size() == expected.size());
for (size_t i = 0; i < items.size(); i++){
assert(items[i] == expected[i]);
}
return 0;
}
type ItemsSorter struct {
items []Item
c func(x, y Item) bool
}
func (s ItemsSorter) Len() int { return len(s.items) }
func (s ItemsSorter) Less(i, j int) bool { return s.c(s.items[i], s.items[j]) }
func (s ItemsSorter) Swap(i, j int) { s.items[i], s.items[j] = s.items[j], s.items[i] }
func sortItems(items []Item, c func(x, y Item) bool) {
sorter := ItemsSorter{
items,
c,
}
sort.Sort(sorter)
}
type ItemCSorter []Item
func (s ItemCSorter) Len() int { return len(s) }
func (s ItemCSorter) Less(i, j int) bool { return c(s[i], s[j]) }
func (s ItemCSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func sortItems(items []Item) {
sorter := ItemCSorter(items)
sort.Sort(sorter)
}
[items sortUsingComparator:c];
items.sort!{|a,b| a-b }
val c: Ordering[Int] = Ordering.Int
val sortedItems = items.sorted(c)
// Or using implicits:
val sortedItems = items.sorted
items.Sort(c);
Array.Sort(items, c);
var orderdEnumerable = x.OrderBy(y => y, c);
table.sort(items,c)
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;
int c(const void *a,const void *b) { int x = *(const int *)a; int y = *(const int *)b; if (x < y) return -1; if (x > y) return +1; return 0; } int main(void) { int arr[]={1,6,3,7,2}; qsort(arr,sizeof(arr)/sizeof(*arr),sizeof(*arr),c); return 0; }
(sort c items)
std::ranges::sort(items, c);
struct is_less { bool operator () (int a, int b){ return a < b; } }; int main(){ std::vector<int> items = {1337, 666, -666, 0, 0, 666, -666}; std::sort(items.begin(), items.end(), is_less()); std::vector<int> expected = {-666, -666, 0, 0, 666, 666, 1337}; assert(items.size() == expected.size()); for (size_t i = 0; i < items.size(); i++){ assert(items[i] == expected[i]); } return 0; }
items.sort!c;
items.sort(c);
Enum.sort(items, c)
lists:sort(C, List).
type ItemsSorter struct { items []Item c func(x, y Item) bool } func (s ItemsSorter) Len() int { return len(s.items) } func (s ItemsSorter) Less(i, j int) bool { return s.c(s.items[i], s.items[j]) } func (s ItemsSorter) Swap(i, j int) { s.items[i], s.items[j] = s.items[j], s.items[i] } func sortItems(items []Item, c func(x, y Item) bool) { sorter := ItemsSorter{ items, c, } sort.Sort(sorter) }
sort.Slice(items, func(i, j int) bool { return c(items[i], items[j]) })
type ItemCSorter []Item func (s ItemCSorter) Len() int { return len(s) } func (s ItemCSorter) Less(i, j int) bool { return c(s[i], s[j]) } func (s ItemCSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func sortItems(items []Item) { sorter := ItemCSorter(items) sort.Sort(sorter) }
slices.SortFunc(items, c)
result = sortBy c items
items.sort(c);
Collections.sort(items, c);
Arrays.sort(items, c);
items.sortWith(c)
(sort items #'c)
[items sortUsingComparator:c];
uasort($items, $c);
with TList.Create do try Sort(c); finally Free; end;
sub c { $a <=> $b; } my @result = sort c @items;
items.sort(key=functools.cmp_to_key(c))
items.sort(key=c)
items.sort!(&c)
items.sort!{|a,b| a-b }
items.sort_by(c);
val c: Ordering[Int] = Ordering.Int val sortedItems = items.sorted(c) // Or using implicits: val sortedItems = items.sorted
| c | c := [ :a :b | a size <= b size ]. " example c for strings " #('a' 'aaa' 'a' 'aaaa') asSortedCollection: c. " => SortedCollection('a' 'a' 'aaa' 'aaaa') "