sort
sort(array &$array, int $flags = SORT_REGULAR): true
对数组元素就地升序排序。
C C23
C23
#include <stdio.h>
#include <stdlib.h>
static int cmp_int(const void *a, const void *b) { // qsort 需要比较回调
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");
// 六个元素:qsort 升序后倒序遍历即降序输出
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;
}
输出
[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()); // 原地排序
std::cout << "[";
for (size_t i = 0; i < a.size(); ++i) {
if (i) std::cout << ", ";
std::cout << a[i];
}
std::cout << "]\n";
// 六个元素降序:std::sort 配 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;
}
输出
[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)}]");
// 六个元素降序:Sort 配比较器
var b = new List<int> { 5, 42, 7, 1, 33, 16 };
b.Sort((x, y) => y.CompareTo(x));
Console.WriteLine($"[{string.Join(", ", b)}]");
输出
[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)
// 六个元素降序:sort.Slice 配自定义比较函数
b := []int{5, 42, 7, 1, 33, 16}
sort.Slice(b, func(i, j int) bool { return b[i] > b[j] })
fmt.Println(b)
}
输出
[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));
// 六个元素降序:对象数组可用反转比较器
Integer[] b = {5, 42, 7, 1, 33, 16};
Arrays.sort(b, java.util.Collections.reverseOrder());
System.out.println(Arrays.toString(b));
}
}
输出
[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);
// 六个元素降序:比较器符号反过来
const b = [5, 42, 7, 1, 33, 16];
b.sort((x, y) => y - x);
console.log(b);
输出
[ 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";
// 六个元素降序:sort() 只升序,rsort() 是反向版
$b = [5, 42, 7, 1, 33, 16];
rsort($b);
echo json_encode($b);
输出
[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) # 六个元素降序
print(b)
输出
[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);
// 六个元素降序:sort_by 反转比较
let mut b = vec![5, 42, 7, 1, 33, 16];
b.sort_by(|x, y| y.cmp(x));
println!("{:?}", b);
}
输出
[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); // 六个元素降序
console.log(b);
输出
[ 1, 2, 3 ] [ 42, 33, 16, 7, 5, 1 ]
语言差异说明
C:以 qsort() 配合比较函数指针(回调)排序。
C++:用 std::sort 就地排序。
C#:List<T>.Sort() 就地升序排序。
Go:sort.Ints 针对 []int。
Java:Arrays.sort。
JavaScript:默认按字符串比较,对数字必须传比较函数。
PHP:sort 就地排序并重建索引。
Rust:.sort() 就地。