Pipes and text
Linux commands: sort
Learn how the Linux sort command orders the lines of a file or piped input, with -r to reverse, -n for numeric order, and -u to drop duplicate lines.
The sort command orders the lines of a text file, or of whatever you pipe into it.
Suppose you have a text file which contains the names of dogs:

This list is unordered.
sort dogs.txt prints the same names in alphabetical order:

Notice that sort does not touch the file. It reads it and prints the sorted lines. If you want to save the result, redirect it to a new file: sort dogs.txt > sorted.txt.
Use the -r option to reverse the order:

Sorting by default is case sensitive, and alphabetic. Use the --ignore-case option to sort without caring about case, and the -n option to sort using a numeric order.
That -n matters more than it looks. Sorting the numbers 10, 9 and 100 alphabetically gives you 10 100 9, because 1 comes before 9 as a character. With -n you get 9 10 100, which is what you meant. I forget this every time I sort file sizes.
If the file contains duplicate lines:

you can use the -u option to remove them:

sort does not just work on files. Like many UNIX commands it also works with pipes, so you can use it on the output of another command. For example you can order the files returned by ls with:
ls | sort
This is where sort shines. Combine it with uniq -c to count occurrences, or with head to get the top entries of a list. We’ll see the uniq combination in the next lesson.
sort is very powerful and has lots more options, which you can explore by calling man sort:

The sort command works on Linux, macOS, WSL, and anywhere you have a UNIX environment
If you have a list in your clipboard and want to sort or dedupe it without the terminal, I built a free line sorter that does both in the browser.
Lesson completed