Getting sorted output out of apt-cache

One annoying thing about Debian is that apt-cache prints the list of packages unordered. The solution, easily enough, is to pipe the output to sort, and this is not only the popular recommendation but also the official stance. It is also stupid. Computers should be subordinate to humans, not the other way around. I have not once needed the unordered output from apt-cache, but I need the output ordered virtually every time (when there are very few results, order does not matter). Why do I have to constantly pipe apt-cache output to sort?

Another utilty that does not order output by default is find. But find has an argument that sorts the output, -s, and achieving sorted output by default is as simple as placing the following line into one's .zshrc:

alias find='find -s'

No such luck with apt-cache.

I finally got tired of having to sort apt-cache's output manually and looked into what it would take to have the output be ordered by the utility. Turns out, not a lot.

This is the entire patch.

Here is how to rebuild your apt with this patch. # means a root shell, % means an unprivileged user's shell.

Install dependencies as root, or use sudo:

# apt-get build-dep apt

Create a new directory and change to it, then get apt's source:

% apt-get source apt

Get and apply the patch:

% cd apt-*
% wget
% patch -p1 <apt-cache-sorted-output-patch.diff

Build the new apt:

% dpkg-buildpackage

The build will fail at the end with a failure to sign the package. Ignore the error and navigate up one level where you will find several .deb files. Install apt_.deb:

# dpkg -i apt_*.deb

Enjoy sorted output from apt-cache.