When sorting always ask if numerical or alphabetical

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the algorithms category.

Last Updated: 2021-05-15

Whenever sorting, always ask if you are doing numerical or alphabetical sorting.

e.g. I wanted to find the biggest files in the project_s repo. So I ran du

$ du project_s
192./.composer/cache/files/phpspec
16 ./.composer/cache/files/fideloper/proxy
1632 ./.composer/cache/files/maximebf
84496 ./.composer/cache/files
854328 ./.composer/cache/repo/https---repo.packagist.org
854328 ./.composer/cache/repo
938832 ./.composer/cache
938840 ./.composer
2810896 .

Next I sorted by piping into sort and sorting on first key

$ du project_s | sort -k 1
968 ./node_modules/jsdom/node_modules/acorn/dist
9696 ./node_modules/terser
971280 ./node_modules
976 ./node_modules/handlebars/dist/amd/handlebars/compiler
984 ./node_modules/array-includes/node_modules/es-abstract/2019
9912 ./node_modules/lodash
992 ./vendor/phpunit/phpunit/tests/end-to-end/regression

As you can see, the order was not what I expected because sort expected text not numbers. Therefore I had to tell sort to sort numerically with

sort -k 1 -n
$ du project_s | sort -k 1 -n

968 ./node_modules/jsdom/node_modules/acorn/dist
976 ./node_modules/handlebars/dist/amd/handlebars/compiler
984 ./node_modules/array-includes/node_modules/es-abstract/2019
992 ./vendor/phpunit/phpunit/tests/end-to-end/regression
9696 ./node_modules/terser
9912 ./node_modules/lodash
971280 ./node_modules

Note that this applies within vim too - e.g. ! sort -n