Skip to main content

Mastering the `sort` Command in Linux

Version sorting and dictionary sorting are two advanced applications of the sort command that can be incredibly useful in a variety of contexts. Here's an explanation of each along with examples and instructions on how to create the necessary files using vim.

Version Sorting with sort​

Version sorting allows you to sort version numbers correctly, which is especially useful when dealing with files or directories named after version schemes.

Example Data for Version Sorting:

  1. Open vim to create a file with version numbers:
    vim versions.txt
  2. Enter the following version numbers (in no particular order):
    1.0.2
    1.0.1
    1.0.10
    2.0
    1.1
  3. Save and exit the file (:wq in vim).

To perform version sort, use the -V option:

sort -V versions.txt

This will sort the version numbers correctly, considering each segment of the version numbers.

Dictionary Sorting with sort​

Dictionary sorting is used to sort text in a way that is more natural for human language, ignoring special characters and considering only blanks and alphanumeric characters.

Example Data for Dictionary Sorting:

  1. Open vim to create a file with dictionary-like entries:
    vim dictionary.txt
  2. Enter a mix of words with special characters, in no particular order:
    apple
    banana
    apple-pie
    banana+shake
    cherry
  3. Save and exit the file (:wq in vim).

To perform dictionary sort, use the -d option:

sort -d dictionary.txt

This will sort the lines in a dictionary order, ignoring the '+' and '-' characters.

What Can You Do Next 🙏😊​

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle