arsort
arsort(array &$array, int $flags = SORT_REGULAR): true
Sorts an array in place descending by value while keeping key-value association.
C C23
#include <stdio.h>
#include <stdlib.h>
typedef struct {
const char *name;
int score;
} Pair;
static int by_score_desc(const void *a, const void *b) {
return ((const Pair *)b)->score - ((const Pair *)a)->score; // swapped operands: descending
}
int main(void) {
Pair ps[] = {{"ada", 88}, {"linus", 95}, {"grace", 72}};
size_t n = sizeof ps / sizeof ps[0];
qsort(ps, n, sizeof ps[0], by_score_desc);
for (size_t i = 0; i < n; i++) {
printf("%s=%d\n", ps[i].name, ps[i].score);
}
return 0;
}
linus=95 ada=88 grace=72
C++ C++26
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
int main() {
std::vector<std::pair<std::string, int>> scores{{"ada", 88}, {"linus", 95}, {"grace", 72}};
std::sort(scores.begin(), scores.end(),
[](const auto &a, const auto &b) { return a.second > b.second; });
for (const auto &[name, score] : scores) std::cout << name << "=" << score << "\n";
return 0;
}
linus=95 ada=88 grace=72
C# v11.0.100
using System;
using System.Collections.Generic;
using System.Linq;
var scores = new Dictionary<string, int> { ["ada"] = 88, ["linus"] = 95, ["grace"] = 72 };
foreach (var kv in scores.OrderByDescending(kv => kv.Value))
{
Console.WriteLine($"{kv.Key}={kv.Value}");
}
linus=95 ada=88 grace=72
Go v1.27.4
package main
import (
"fmt"
"sort"
)
func main() {
scores := map[string]int{"ada": 88, "linus": 95, "grace": 72}
names := make([]string, 0, len(scores))
for name := range scores {
names = append(names, name)
}
sort.Slice(names, func(i, j int) bool {
return scores[names[i]] > scores[names[j]] // descending
})
for _, name := range names {
fmt.Printf("%s=%d\n", name, scores[name])
}
}
linus=95 ada=88 grace=72
Java v27.0.1
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new HashMap<>();
scores.put("ada", 88);
scores.put("linus", 95);
scores.put("grace", 72);
var entries = new ArrayList<>(scores.entrySet());
entries.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));
Map<String, Integer> sorted = new LinkedHashMap<>(); // rebuild in sorted order
for (var e : entries) sorted.put(e.getKey(), e.getValue());
System.out.println(sorted);
}
}
{linus=95, ada=88, grace=72}
JavaScript v26.13.1
const scores = { ada: 88, linus: 95, grace: 72 };
const sorted = Object.fromEntries(Object.entries(scores).sort((a, b) => b[1] - a[1]));
console.log(sorted);
{ linus: 95, ada: 88, grace: 72 }
PHP v8.6.0
$scores = ["ada" => 88, "linus" => 95, "grace" => 72];
arsort($scores); // descending by value, keeps key-value association
foreach ($scores as $name => $score) {
echo $name, "=", $score, "\n";
}
linus=95 ada=88 grace=72
Python v3.15.2
scores = {"ada": 88, "linus": 95, "grace": 72}
print(dict(sorted(scores.items(), key=lambda kv: kv[1], reverse=True)))
{'linus': 95, 'ada': 88, 'grace': 72}
Rust v1.99.1
fn main() {
let mut scores = vec![("ada", 88), ("linus", 95), ("grace", 72)];
scores.sort_by_key(|&(_, score)| std::cmp::Reverse(score));
for (name, score) in &scores {
println!("{}={}", name, score);
}
}
linus=95 ada=88 grace=72
TypeScript v26.13.1
const scores: Record<string, number> = { ada: 88, linus: 95, grace: 72 };
const sorted = Object.fromEntries(Object.entries(scores).sort((a, b) => b[1] - a[1]));
console.log(sorted);
{ linus: 95, ada: 88, grace: 72 }
Language differences
C: swaps the operands in a hand-written comparator — note that b - a subtraction can overflow near extreme values; the safe form is a three-way comparison like ((b>a)-(b<a)).
C++: swaps the operands in a hand-written comparator.
C#: uses OrderByDescending.
Go: the less predicate becomes >.
Java: wraps Comparator.reverseOrder().
JavaScript: flips the comparator to b - a.
PHP: arsort is the leaderboard workhorse — descending by value with keys intact (the key IS the name on the leaderboard; rsort rebuilding indexes would lose it); family mapping: asort (ascending by value), rsort (descending by value but re-indexes), krsort (descending by key), uasort (custom callback, keys kept); the demo uses three distinct scores so stability differences can't affect the output.
Python: passes reverse=True.
Rust: wraps the key in std::cmp::Reverse.
TypeScript: same as JavaScript — flip the comparator to b - a.