sort
put lines in ordersort reads lines and prints them in order. By default that is alphabetical, which puts 10 before 9; -n sorts by number and -h understands sizes such as 2K and 1G.
Text order is not number order: use -n for numbers and -h for sizes.
At the terminal
Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.
What the numbered lines are telling you
Compared as text, "10" comes before "9" because the character 1 comes before 9.
Now 9 is first. This is almost always what is wanted for a column of figures.
-t, makes the comma the separator and -k4 sorts on the fourth field. -n is numeric and -r reverses: highest salary first.
Switches
A switch changes how the command behaves. These are the ones used day to day; man sort lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.
-n- Numeric: compare as numbers.
-h- Human-numeric: understand sizes such as 2K and 1G.
-r- Reverse the order.
-u- Unique: drop duplicate lines.
-k NUM- Sort by field number NUM.
-t CHAR- Use this character as the field separator.
-f- Fold case: treat upper and lower case alike.
Variations worth knowing
The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.
du -sh * | sort -hSorts readable sizes correctly, so 900K comes before 2M.
sort -u names.txtSorts and removes duplicate lines in one step.
sort -k2 file.txtSorts on the second whitespace-separated field.
sort access.log | uniq -c | sort -nr | headThe classic pipeline: the ten most frequent lines.
In cybersecurity
Sorting is the first step in making sense of a log.
Extract the address column from an access log, sort it, count with uniq -c and sort by the count: the top talkers appear first.
diff and comm need sorted input. Sorting two lists of users or packages is what makes comparing them possible.
Sorting rules depend on the language setting, so two machines can sort the same file differently. LC_ALL=C sort gives a fixed byte order.