array_multisort
array_multisort(array &...$arrays, mixed ...$args): bool
Sorts multiple arrays jointly in the given order, keeping elements aligned across arrays.
C C23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// A single qsort call with a multi-field comparator: primary key descending, secondary key ascending
typedef struct {
char name[8];
int score;
} Pair;
static int cmp(const void *a, const void *b) {
const Pair *x = a, *y = b;
if (x->score != y->score) return y->score - x->score;
return strcmp(x->name, y->name);
}
int main(void) {
Pair pairs[] = {{"ada", 90}, {"bob", 85}, {"cyd", 90}, {"eve", 70}};
size_t n = sizeof pairs / sizeof pairs[0];
qsort(pairs, n, sizeof pairs[0], cmp);
printf("[");
for (size_t i = 0; i < n; i++) printf(i ? ", %d" : "%d", pairs[i].score);
printf("]\n[");
for (size_t i = 0; i < n; i++) printf(i ? ", %s" : "%s", pairs[i].name);
printf("]");
return 0;
}
[90, 90, 85, 70] [ada, cyd, bob, eve]
C++ C++26
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
int main() {
// A multi-field comparator written in one lambda: score descending, name ascending
std::vector<std::pair<std::string, int>> pairs = {
{"ada", 90}, {"bob", 85}, {"cyd", 90}, {"eve", 70}};
std::sort(pairs.begin(), pairs.end(), [](auto &x, auto &y) {
if (x.second != y.second) return x.second > y.second;
return x.first < y.first;
});
std::cout << "[";
for (size_t i = 0; i < pairs.size(); ++i) std::cout << (i ? ", " : "") << pairs[i].second;
std::cout << "]\n[";
for (size_t i = 0; i < pairs.size(); ++i) std::cout << (i ? ", " : "") << pairs[i].first;
std::cout << "]";
return 0;
}
[90, 90, 85, 70] [ada, cyd, bob, eve]
C# v11.0.100
// LINQ: OrderByDescending(primary).ThenBy(secondary), not in place — produces a new sequence
var pairs = new[] { ("ada", 90), ("bob", 85), ("cyd", 90), ("eve", 70) };
var ordered = pairs.OrderByDescending(p => p.Item2).ThenBy(p => p.Item1).ToArray();
Console.WriteLine("[" + string.Join(", ", ordered.Select(p => p.Item2)) + "]");
Console.WriteLine("[" + string.Join(", ", ordered.Select(p => p.Item1)) + "]");
[90, 90, 85, 70] [ada, cyd, bob, eve]
Go v1.27.4
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
type pair struct {
name string
score int
}
func main() {
// sort.SliceStable: primary/secondary key comparison in a single Less
pairs := []pair{{"ada", 90}, {"bob", 85}, {"cyd", 90}, {"eve", 70}}
sort.SliceStable(pairs, func(i, j int) bool {
if pairs[i].score != pairs[j].score {
return pairs[i].score > pairs[j].score
}
return pairs[i].name < pairs[j].name
})
scores := make([]string, len(pairs))
names := make([]string, len(pairs))
for i, p := range pairs {
scores[i] = strconv.Itoa(p.score)
names[i] = p.name
}
fmt.Println("[" + strings.Join(scores, ", ") + "]")
fmt.Println("[" + strings.Join(names, ", ") + "]")
}
[90, 90, 85, 70] [ada, cyd, bob, eve]
Java v27.0.1
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Comparator chain: comparing(primary).reversed().thenComparing(secondary)
String[][] pairs = {{"ada", "90"}, {"bob", "85"}, {"cyd", "90"}, {"eve", "70"}};
Arrays.sort(pairs, Comparator.comparing((String[] p) -> Integer.parseInt(p[1]))
.reversed()
.thenComparing(p -> p[0]));
List<String> scores = new ArrayList<>();
List<String> names = new ArrayList<>();
for (String[] p : pairs) {
scores.add(p[1]);
names.add(p[0]);
}
System.out.println("[" + String.join(", ", scores) + "]");
System.out.println("[" + String.join(", ", names) + "]");
}
}
[90, 90, 85, 70] [ada, cyd, bob, eve]
JavaScript v26.13.1
// JS has no multi-key sort: exploit sort's mandated stability — sort by secondary key first, then primary
const pairs = [["ada", 90], ["bob", 85], ["cyd", 90], ["eve", 70]];
pairs.sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0)); // Secondary key: name ascending
pairs.sort((x, y) => y[1] - x[1]); // Primary key: score descending (stable sort preserves the order within equal scores)
console.log("[" + pairs.map((p) => p[1]).join(", ") + "]");
console.log("[" + pairs.map((p) => p[0]).join(", ") + "]");
[90, 90, 85, 70] [ada, cyd, bob, eve]
PHP v8.6.0
// array_multisort: joint sort with $scores descending as the primary key and $names ascending as the secondary
$names = ["ada", "bob", "cyd", "eve"];
$scores = [90, 85, 90, 70];
array_multisort($scores, SORT_DESC, $names, SORT_ASC);
echo "[", implode(", ", $scores), "]\n";
echo "[", implode(", ", $names), "]";
[90, 90, 85, 70] [ada, cyd, bob, eve]
Python v3.15.2
# sorted + key tuple: (-score, name) completes the multi-key joint sort in one pass (negative score for descending)
pairs = [("ada", 90), ("bob", 85), ("cyd", 90), ("eve", 70)]
ordered = sorted(pairs, key=lambda p: (-p[1], p[0]))
scores = [p[1] for p in ordered]
names = [p[0] for p in ordered]
print("[" + ", ".join(map(str, scores)) + "]")
print("[" + ", ".join(names) + "]")
[90, 90, 85, 70] [ada, cyd, bob, eve]
Rust v1.99.1
// sort_by_key with a tuple key: Reverse(score) descending + name ascending, a joint sort in one pass
use std::cmp::Reverse;
fn main() {
let mut pairs = vec![("ada", 90), ("bob", 85), ("cyd", 90), ("eve", 70)];
pairs.sort_by_key(|p| (Reverse(p.1), p.0));
let scores: Vec<String> = pairs.iter().map(|p| p.1.to_string()).collect();
let names: Vec<&str> = pairs.iter().map(|p| p.0).collect();
println!("[{}]", scores.join(", "));
println!("[{}]", names.join(", "));
}
[90, 90, 85, 70] [ada, cyd, bob, eve]
TypeScript v26.13.1
// two-pass stable sort: secondary key first, then the primary key
const pairs: [string, number][] = [["ada", 90], ["bob", 85], ["cyd", 90], ["eve", 70]];
pairs.sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0));
pairs.sort((x, y) => y[1] - x[1]);
console.log("[" + pairs.map((p) => p[1]).join(", ") + "]");
console.log("[" + pairs.map((p) => p[0]).join(", ") + "]");
[90, 90, 85, 70] [ada, cyd, bob, eve]
Language differences
C: no joint-sort function — build a struct array and write primary/secondary keys in one comparator (shown), or swap two parallel arrays in lockstep.
C++: std::sort with a multi-field lambda (std::stable_sort when stability matters); PHP's parallel-array linkage is usually modeled with pairs/vectors.
C#: LINQ OrderByDescending(...).ThenBy(...) chains the keys; it returns a new sequence, unlike array_multisort's in-place mutation.
Go: sort.SliceStable writes both keys in one Less function; parallel slices need an index-based wrapper.
Java: Comparator.comparing(primary).reversed().thenComparing(secondary) is the standard answer.
JavaScript: no joint-sort API — since ES2019 sort is guaranteed stable, so sort by the secondary key first, then the primary.
PHP: array_multisort is a PHP signature: elements of several independent arrays are reordered in lockstep by position (the classic sort-a-table-by-a-column use); each array takes SORT_ASC/SORT_DESC plus comparison flags.
Python: the idiom is sorted(key=lambda ...) returning a tuple key (negate numbers for descending) — one expression, many keys.
Rust: sort_by_key with a (Reverse(score), name) tuple key; use sort_by when a key won't do.
TypeScript: same two-pass stable sort as JavaScript, or the tuple-key idea shared with Rust.