Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Ada
- C
- Clojure
- C++
- C++
- C#
- C#
- C#
- D
- Dart
- Elixir
- Erlang
- Go
- Go
- Go
- Go
- Haskell
- JS
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Ruby
- Rust
- Scala
- Smalltalk
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;
}
The comparison is often written as "return x - y;" instead which is broken due to possible integer over/underflow.
(sort c items)
if items is a Java array, it gets sorted in place
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;
}
c is a bad name for a comparator because it is not descriptive. is_less is a better name.
The initializer list for items requires the compiler parameter -std=c++11
std::sort might not be stable. If you want a stable sorting algorithm, use std::stable_sort instead.
The initializer list for items requires the compiler parameter -std=c++11
std::sort might not be stable. If you want a stable sorting algorithm, use std::stable_sort instead.
items.Sort(c);
items is a List<Item>
c is an implementation of IComparer<Item>
c is an implementation of IComparer<Item>
Array.Sort(items, c);
items is an array of type Item[].
c implements IComparer<Item>
c implements IComparer<Item>
items.sort(c);
Enum.sort(items, c)
lists:sort(C, List).
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)
}
c has type func(Item, Item) bool.
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)
}
ItemsSorter contains c, which can be any comparator decided at runtime.
items.sortWith(c)
(sort items #'c)
;;; c is a predicate like (defun c ( u v ) ( > u v ))
;;; items is a list
;;; the sorted list is
;;; note that #'c denotes the function c.
;;; items is a list
;;; the sorted list is
;;; note that #'c denotes the function c.
[items sortUsingComparator:c];
Sorts in-place; for creation of a new sorted array leaving the receiver unchanged there's sortedArrayUsingComparator:
items.sort!(&c)
Idiomatic would be using a block instead of named procedure c.
items.sort!{|a,b| a-b }
sort! sorts in_place (sort would leave items unmodified). Comparator c is not idiomatic, items is sorted according to the code in the block.
val c: Ordering[Int] = Ordering.Int
val sortedItems = items.sorted(c)
// Or using implicits:
val sortedItems = items.sorted
The lines creating & using the variable c can be excluded, as seen below.
The argument c is an "implicit argument", which means that if a suitable, unique, implementation of `Ordering[Int]` can be found, then the argument list can be excluded.
The argument c is an "implicit argument", which means that if a suitable, unique, implementation of `Ordering[Int]` can be found, then the argument list can be excluded.
| c |
c := [ :a :b | a size <= b size ]. " example c for strings "
#('a' 'aaa' 'a' 'aaaa') asSortedCollection: c.
" => SortedCollection('a' 'a' 'aaa' 'aaaa') "