rsort

rsort(array &$array, int $flags = SORT_REGULAR): true

Sorts array elements in place in descending order.

C C23

C23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// qsort has no descending switch: negate the comparator's return value
static int cmp_desc(const void *a, const void *b) {
    return *(const int *)b - *(const int *)a;
}

static int cmp_str_desc(const void *a, const void *b) {
    return strcmp(*(const char **)b, *(const char **)a);
}

int main(void) {
    int a[] = {5, 42, 7, 1, 33, 16};
    size_t n = sizeof a / sizeof a[0];
    qsort(a, n, sizeof a[0], cmp_desc);
    printf("[");
    for (size_t i = 0; i < n; i++) printf(i ? ", %d" : "%d", a[i]);
    printf("]\n");
    const char *s[] = {"banana", "apple", "cherry"};
    size_t ns = sizeof s / sizeof s[0];
    qsort(s, ns, sizeof s[0], cmp_str_desc);
    printf("[");
    for (size_t i = 0; i < ns; i++) printf(i ? ", %s" : "%s", s[i]);
    printf("]");
    return 0;
}
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

C++ C++26

C++26
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>

int main() {
    // The std::greater<T>() comparator means descending
    std::vector<int> a = {5, 42, 7, 1, 33, 16};
    std::sort(a.begin(), a.end(), std::greater<int>());
    std::cout << "[";
    for (size_t i = 0; i < a.size(); ++i) std::cout << (i ? ", " : "") << a[i];
    std::cout << "]\n";
    std::vector<std::string> s = {"banana", "apple", "cherry"};
    std::sort(s.begin(), s.end(), std::greater<std::string>());
    std::cout << "[";
    for (size_t i = 0; i < s.size(); ++i) std::cout << (i ? ", " : "") << s[i];
    std::cout << "]";
    return 0;
}
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

C# v11.0.100

v11.0.100
using System.Collections.Generic;

// List.Sort with a reversing comparator (or LINQ OrderByDescending for a new sequence)
var a = new List<int> { 5, 42, 7, 1, 33, 16 };
a.Sort((x, y) => y.CompareTo(x));
Console.WriteLine("[" + string.Join(", ", a) + "]");
var s = new List<string> { "banana", "apple", "cherry" };
s.Sort((x, y) => y.CompareTo(x));
Console.WriteLine("[" + string.Join(", ", s) + "]");
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"sort"
	"strconv"
	"strings"
)

func main() {
	// Go has no descending sort: sort.Reverse wraps any sort interface
	a := []int{5, 42, 7, 1, 33, 16}
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
	fmt.Println("[" + intsJoin(a) + "]")
	s := []string{"banana", "apple", "cherry"}
	sort.Sort(sort.Reverse(sort.StringSlice(s)))
	fmt.Println("[" + strings.Join(s, ", ") + "]")
}

func intsJoin(a []int) string {
	parts := make([]string, len(a))
	for i, v := range a {
		parts[i] = strconv.Itoa(v)
	}
	return strings.Join(parts, ", ")
}
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

Java v27.0.1

v27.0.1
import java.util.Arrays;
import java.util.Collections;

public class Main {
    public static void main(String[] args) {
        // Primitive arrays have no descending sort: box them and use the reverseOrder comparator
        Integer[] a = {5, 42, 7, 1, 33, 16};
        Arrays.sort(a, Collections.reverseOrder());
        System.out.println("[" + String.join(", ", Arrays.stream(a).map(String::valueOf).toArray(String[]::new)) + "]");
        String[] s = {"banana", "apple", "cherry"};
        Arrays.sort(s, Collections.reverseOrder());
        System.out.println("[" + String.join(", ", s) + "]");
    }
}
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

JavaScript v26.13.1

v26.13.1
// JS has no rsort: sort with a reversing comparator (numeric y-x, lexicographic inverted)
const a = [5, 42, 7, 1, 33, 16];
a.sort((x, y) => y - x);
console.log("[" + a.join(", ") + "]");
const s = ["banana", "apple", "cherry"];
s.sort((x, y) => (x < y ? 1 : x > y ? -1 : 0));
console.log("[" + s.join(", ") + "]");
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

PHP v8.6.0

v8.6.0
$a = [5, 42, 7, 1, 33, 16];
rsort($a); // Descending in place with reindexed keys
echo "[", implode(", ", $a), "]\n";
$s = ["banana", "apple", "cherry"];
rsort($s, SORT_STRING); // String descending
echo "[", implode(", ", $s), "]";
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

Python v3.15.2

v3.15.2
a = [5, 42, 7, 1, 33, 16]
a.sort(reverse=True)  # reverse=True means descending
print("[" + ", ".join(map(str, a)) + "]")
s = ["banana", "apple", "cherry"]
s.sort(reverse=True)
print("[" + ", ".join(s) + "]")
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

Rust v1.99.1

v1.99.1
fn main() {
    // No rsort: sort_by with a reversed comparison (or sort then reverse)
    let mut a = vec![5, 42, 7, 1, 33, 16];
    a.sort_by(|x, y| y.cmp(x));
    let strs: Vec<String> = a.iter().map(|v| v.to_string()).collect();
    println!("[{}]", strs.join(", "));
    let mut s = vec!["banana", "apple", "cherry"];
    s.sort_by(|x, y| y.cmp(x));
    println!("[{}]", s.join(", "));
}
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

TypeScript v26.13.1

v26.13.1
// no rsort: sort with a reversed comparator
const a: number[] = [5, 42, 7, 1, 33, 16];
a.sort((x, y) => y - x);
console.log("[" + a.join(", ") + "]");
const s: string[] = ["banana", "apple", "cherry"];
s.sort((x, y) => (x < y ? 1 : x > y ? -1 : 0));
console.log("[" + s.join(", ") + "]");
Output
[42, 33, 16, 7, 5, 1]
[cherry, banana, apple]

Language differences

C: qsort has no descending switch — flip the comparator (negate for numbers, swap strcmp arguments for strings).

C++: std::sort with std::greater<T>() sorts descending; sort-then-reverse also works.

C#: List<T>.Sort((x, y) => y.CompareTo(x)); LINQ OrderByDescending produces a new sequence (not in place).

Go: sort.Reverse wraps any sort.Interface for reversed order — more general than writing a negated Less.

Java: primitive arrays only sort ascending via Arrays.sort; object arrays use the Collections.reverseOrder() comparator.

JavaScript: no rsort — sort((x, y) => y - x) flips the comparator; for strings just reverse the comparison.

PHP: rsort is sort's descending mirror: in place, rebuilding numeric indexes (use arsort to keep key association); the second argument (SORT_STRING, ...) controls comparison, returning true since PHP 8.

Python: no separate function — sort(reverse=True) (or non-in-place sorted(..., reverse=True)) is the only idiom.

Rust: sort_by(|x, y| y.cmp(x)) reverses the comparison; integers can also use sort_unstable_by_key(|x| Reverse(*x)).

TypeScript: same as JavaScript — flip the comparator.