Logo

Programming-Idioms

History of Idiom 100 > diff from v15 to v16

Edit summary for version 16 by :
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.

Imports
with Ada.Containers.Vectors;
use Ada.Containers;
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;
Imports
#include <stdlib.h>
Imports
#include <stdlib.h>
Code
int c(const void *a,const void *b)
{
	const int *ap=(const int *)a;
	const int *bp=(const int *)b;
	return *a-*b;
}

int main(void)
{
	int arr[]={1,6,3,7,2};
	qsort(arr,sizeof(arr)/sizeof(*arr),sizeof(*arr),c);

	return 0;
}
Code
int c(const void *a,const void *b)
{
	const int *ap=(const int *)a;
	const int *bp=(const int *)b;
	return *a-*b;
}

int main(void)
{
	int arr[]={1,6,3,7,2};
	qsort(arr,sizeof(arr)/sizeof(*arr),sizeof(*arr),c);

	return 0;
}
Imports
import "sort"
Imports
import "sort"
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
c has type func(Item, Item) bool.
Comments bubble
c has type func(Item, Item) bool.
Doc URL
https://golang.org/pkg/sort/#example__sortKeys
Doc URL
https://golang.org/pkg/sort/#example__sortKeys
Demo URL
http://play.golang.org/p/1cJqFz0kbS
Demo URL
http://play.golang.org/p/1cJqFz0kbS
Imports
import java.util.Collections;
import java.util.Comparator;
Imports
import java.util.Collections;
import java.util.Comparator;
Code
Collections.sort(items, c);
Code
Collections.sort(items, c);
Comments bubble
items is a List<Item>.
c implements Comparator<Item>.
Comments bubble
items is a List<Item>.
c implements Comparator<Item>.
Doc URL
http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
Doc URL
http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
Demo URL
http://ideone.com/z0bXdE
Demo URL
http://ideone.com/z0bXdE