This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
- 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.
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>
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 sortUsingComparator:c];
Sorts in-place; for creation of a new sorted array leaving the receiver unchanged there's sortedArrayUsingComparator:
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.
programming-idioms.org