sort
sort(array &$array, int $flags = SORT_REGULAR): true
Sorts array elements in place in ascending order.
C C23
C23
#include <stdio.h>
#include <stdlib.h>
static int cmp_int(const void *a, const void *b) { // qsort requires a comparison callback
int x = *(const int *)a, y = *(const int *)b;
return (x > y) - (x < y);
}
int main(void) {
int a[] = {3, 1, 2};
size_t n = sizeof a / sizeof a[0];
qsort(a, n, sizeof a[0], cmp_int);
printf("[");
for (size_t i = 0; i < n; i++) {
printf(i ? ", %d" : "%d", a[i]);
}
printf("]\n");
// six elements: ascending qsort, then walk backwards for descending output
int b[] = {5, 42, 7, 1, 33, 16};
size_t nb = sizeof b / sizeof b[0];
qsort(b, nb, sizeof b[0], cmp_int);
printf("[");
for (size_t i = nb; i > 0; i--) printf(i == nb ? "%d" : ", %d", b[i - 1]);
printf("]\n");
return 0;
}
Output
[1, 2, 3] [42, 33, 16, 7, 5, 1]
C++ C++26
C++26
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {3, 1, 2};
std::sort(a.begin(), a.end()); // in-place sort
std::cout << "[";
for (size_t i = 0; i < a.size(); ++i) {
if (i) std::cout << ", ";
std::cout << a[i];
}
std::cout << "]\n";
// six elements, descending: std::sort with a comparator lambda
std::vector<int> b = {5, 42, 7, 1, 33, 16};
std::sort(b.begin(), b.end(), [](int x, int y) { return x > y; });
std::cout << "[";
for (size_t i = 0; i < b.size(); ++i) {
if (i) std::cout << ", ";
std::cout << b[i];
}
std::cout << "]\n";
return 0;
}
Output
[1, 2, 3] [42, 33, 16, 7, 5, 1]
C# v11.0.100
v11.0.100
var a = new List<int> { 3, 1, 2 };
a.Sort();
Console.WriteLine($"[{string.Join(", ", a)}]");
// six elements, descending: Sort with a comparator
var b = new List<int> { 5, 42, 7, 1, 33, 16 };
b.Sort((x, y) => y.CompareTo(x));
Console.WriteLine($"[{string.Join(", ", b)}]");
Output
[1, 2, 3] [42, 33, 16, 7, 5, 1]
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"sort"
)
func main() {
a := []int{3, 1, 2}
sort.Ints(a)
fmt.Println(a)
// six elements, descending: sort.Slice with a custom less function
b := []int{5, 42, 7, 1, 33, 16}
sort.Slice(b, func(i, j int) bool { return b[i] > b[j] })
fmt.Println(b)
}
Output
[1 2 3] [42 33 16 7 5 1]
Java v27.0.1
v27.0.1
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = {3, 1, 2};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
// six elements, descending: object arrays can use a reverse comparator
Integer[] b = {5, 42, 7, 1, 33, 16};
Arrays.sort(b, java.util.Collections.reverseOrder());
System.out.println(Arrays.toString(b));
}
}
Output
[1, 2, 3] [42, 33, 16, 7, 5, 1]
JavaScript v26.13.1
v26.13.1
const a = [3, 1, 2];
a.sort((x, y) => x - y);
console.log(a);
// six elements, descending: flip the comparator sign
const b = [5, 42, 7, 1, 33, 16];
b.sort((x, y) => y - x);
console.log(b);
Output
[ 1, 2, 3 ] [ 42, 33, 16, 7, 5, 1 ]
PHP v8.6.0
v8.6.0
$a = [3, 1, 2];
sort($a);
echo json_encode($a), "\n";
// six elements, descending: sort() only ascends, rsort() is the reverse
$b = [5, 42, 7, 1, 33, 16];
rsort($b);
echo json_encode($b);
Output
[1,2,3] [42,33,16,7,5,1]
Python v3.15.2
v3.15.2
a = [3, 1, 2]
a.sort()
print(a)
b = [5, 42, 7, 1, 33, 16]
b.sort(reverse=True) # six elements, descending
print(b)
Output
[1, 2, 3] [42, 33, 16, 7, 5, 1]
Rust v1.99.1
v1.99.1
fn main() {
let mut a = vec![3, 1, 2];
a.sort();
println!("{:?}", a);
// six elements, descending: sort_by with a flipped comparison
let mut b = vec![5, 42, 7, 1, 33, 16];
b.sort_by(|x, y| y.cmp(x));
println!("{:?}", b);
}
Output
[1, 2, 3] [42, 33, 16, 7, 5, 1]
TypeScript v26.13.1
v26.13.1
const a: number[] = [3, 1, 2];
a.sort((x, y) => x - y);
console.log(a);
const b: number[] = [5, 42, 7, 1, 33, 16];
b.sort((x, y) => y - x); // six elements, descending
console.log(b);
Output
[ 1, 2, 3 ] [ 42, 33, 16, 7, 5, 1 ]
Language differences
C: sorts with qsort() plus a comparison function pointer (callback).
C++: sorts in place with std::sort.
C#: List<T>.Sort() sorts in place, ascending.
Go: sort.Ints targets []int.
Java: uses Arrays.sort.
JavaScript: compares as strings by default, so numbers require a comparator function.
PHP: sort sorts in place and rebuilds the indexes.
Rust: .sort() sorts in place.