A list keeps its elements in the order you added them. When you want them in a meaningful order instead, Apex gives you a built-in sort() method that does the work for you.
You call sort() on the list itself:
List<Integer> nums = new List<Integer>{ 30, 10, 20 };
nums.sort();
// nums is now (10, 20, 30)
Notice there is no = sign. The sort() method rearranges the elements inside the list you already have. It does not hand back a new list.
For the built-in types you have seen so far, sort() uses the natural order:
Integer and Decimal sort from smallest to largestString sorts alphabetically, with uppercase letters before lowercase onesDate sorts from earliest to latestList<String> names = new List<String>{ 'Zoe', 'Ana', 'Miguel' };
names.sort();
// names is now (Ana, Miguel, Zoe)
The most common mistake is writing nums = nums.sort();. Because sort() hands back nothing, that line wipes out your list. Call sort() on its own line and keep using the same variable afterwards.
Sorting an empty list is safe. There is nothing to rearrange, so the list simply stays empty.
Sorted data is easier to read and easier to search. Ranking scores from lowest to highest, listing account names alphabetically, or putting dates in order all start with a single call to sort().