Ranking Game: Sort Scores from High to Low
Introduction
In this chapter you will build a small ranking game: sort kindergarten students’ Chinese scores from highest to lowest and print a clear leaderboard. You will combine Arrays, Loops, and Arrays.sort—without implementing bubble sort or other algorithms by hand.
Prerequisites
Project Goal
Requirements:
- Several students, each with a name and Chinese score
- Sort by score high to low
- Display ranking with order numbers
This mirrors real school score reports and leaderboard features.
1) Prepare Data
We group each student’s name and score. A record (Java 16+) is a compact way to hold two fields—you will learn more about classes in OOP overview.
record Student(String name, int score) {}Student[] students = {
new Student("Liam", 89),
new Student("Emma", 95),
new Student("Noah", 78),
new Student("Olivia", 92),
new Student("Ava", 85),
};
System.out.println("Original data:");
for (Student student : students) {
System.out.println(student.name() + " - " + student.score());
}2) Sort by Score Descending with Arrays.sort
Arrays.sort can take a comparator that defines sort order. This example sorts higher scores first:
import java.util.Arrays;
Arrays.sort(students, (a, b) -> Integer.compare(b.score(), a.score()));Reading guide:
Integer.compare(x, y)returns negative / zero / positive likexvsyb.score()beforea.score()means descending by score- The
(a, b) -> ...syntax is a lambda—full detail in Functional Programming. For now, treat it as “sort rule: higher score wins.”
Tip
Key Idea
You do not need to write bubble sort. Arrays.sort is highly optimized; your job is to supply what to compare.
3) Print a Friendly Ranking Table
System.out.println("=== Chinese Score Ranking ===");
for (int i = 0; i < students.length; i++) {
Student student = students[i];
System.out.println((i + 1) + ". " + student.name() + " - " + student.score());
}Example output:
1. Emma - 95
2. Olivia - 92
3. Liam - 89
4. Ava - 85
5. Noah - 784) Full Runnable Version
import java.util.Arrays;
public class RankingGame {
record Student(String name, int score) {}
public static void main(String[] args) {
Student[] students = {
new Student("Liam", 89),
new Student("Emma", 95),
new Student("Noah", 78),
new Student("Olivia", 92),
new Student("Ava", 85),
};
Arrays.sort(students, (a, b) -> Integer.compare(b.score(), a.score()));
System.out.println("=== Chinese Score Ranking ===");
for (int i = 0; i < students.length; i++) {
Student student = students[i];
System.out.println((i + 1) + ". " + student.name() + " - " + student.score());
}
}
}5) Alternative Without record (Parallel Arrays)
If you prefer not to use record yet, keep two arrays in sync manually—but sorting becomes awkward. The record + Arrays.sort approach is the standard Java path for this exercise.
For scores only practice:
int[] scores = {89, 95, 78, 92, 85};
Arrays.sort(scores); // ascending: 78 ... 95
System.out.println("Scores high to low:");
for (int i = scores.length - 1; i >= 0; i--) {
System.out.println(scores[i]);
}This prints scores only; pairing names requires sorting students together (section 2).
6) Upgrade: User Input Mode
import java.util.Arrays;
import java.util.Scanner;
public class RankingGameInput {
record Student(String name, int score) {}
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("How many students? ");
int count = scanner.nextInt();
scanner.nextLine(); // consume newline
Student[] students = new Student[count];
for (int i = 0; i < count; i++) {
System.out.print("Name for student #" + (i + 1) + ": ");
String name = scanner.nextLine().strip();
System.out.print("Chinese score for " + name + ": ");
int score = scanner.nextInt();
scanner.nextLine();
students[i] = new Student(name, score);
}
Arrays.sort(students, (a, b) -> Integer.compare(b.score(), a.score()));
System.out.println("=== Chinese Score Ranking ===");
for (int i = 0; i < students.length; i++) {
Student s = students[i];
System.out.println((i + 1) + ". " + s.name() + " - " + s.score());
}
}
}
}Warning
If two students share the same score, their relative order follows the sort algorithm’s stability rules—often acceptable for beginners. For tie-breaking by name, extend the comparator later.
Surprise Practice: Medal Labels
Add medals for top three:
- 1 →
Gold - 2 →
Silver - 3 →
Bronze - others →
Participant
String medal = switch (rank) {
case 1 -> "Gold";
case 2 -> "Silver";
case 3 -> "Bronze";
default -> "Participant";
};
System.out.println(rank + ". " + student.name() + " - " + student.score() + " (" + medal + ")");You are building a real leaderboard pattern.
Common Beginner Mistakes
Mistake 1: Sorting Ascending by Accident
Arrays.sort(scores) on int[] is ascending. For high-to-low leaderboards, use a comparator on student data or print the int[] backward.
Mistake 2: Names No Longer Match Scores
Sorting only the int[] does not reorder String[] names. Sort students (or indices) together.
Mistake 3: Scanner Line Issues After nextInt
Call nextLine() after numeric input before the next nextLine() for names.
What’s Next
Combine Random, Scanner, loops, and conditionals in Guess the Number Game.
FAQ
Should I use Arrays.sort or Collections.sort?
For arrays, use Arrays.sort. Collections.sort applies to list types like ArrayList in Collections.
Why use record here?
It keeps name and score together so sorting stays correct. You will learn richer class design soon.
Can scores be decimals?
Yes—change int score to double score and adjust Integer.compare to Double.compare.
How do I sort by score then by name?
Extend the comparator: compare scores first; if equal, compare name() (covered more in Functional Programming).
Is writing bubble sort required?
No. This course uses library sorting so you focus on data and output, not reinventing algorithms.