Programming-Idioms

Implementation
Ada

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

Please try to avoid dependencies to third-party libraries and frameworks.

Implementation edit is for fixing errors and enhancing with metadata.

Instead of changing the code of the snippet, consider creating another Ada implementation.

Other implementations
import java.util.Arrays;
import java.util.Comparator;
Arrays.sort(items, c);
import java.util.Collections;
import java.util.Comparator;
Collections.sort(items, c);
import "sort"
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)
}
#include <stdlib.h>
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;
}
fn main()
{
    let c = |a: &u32,b: &u32|a.cmp(b);

    let mut v = vec![1,7,5,2,3];
    v.sort_by(c);
    println!("{:#?}",v);
}
items.sort(c)
items.sort(c);
import std.algorithm: sort;
items.sort!c;
import Data.List
result = sortBy c items
#include <algorithm>
#include <vector>
#include <cassert>
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;
}
uses classes;
with TList.Create do try
  Sort(c);
finally
  Free;
end;
table.sort(items,c)
lists:sort(C, List).
items.sort!{|a,b| a-b }
import "sort"
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)
}
import "sort"
sort.Slice(items, func(i, j int) bool {
	return c(items[i], items[j])
})
Enum.sort(items, c)
sub c {
 $a <=> $b;
}

my @result = sort c @items;

items.sort!(&c)
uasort($items, $c);
items.sort_by(c);
using System;
using System.Linq;
var orderdEnumerable = x.OrderBy(y => y, c);
Array.Sort(items, c);
using System.Collections.Generic;
items.Sort(c);
items.sortWith(c)