Sorting Books

I wrote a little program to sort lists of books.

Over the weekend I was tidying up my note on books to read. I try to track what I want to read, who recommended it, and when they recommended it. I used to just try to manually keep stuff in alphabetical order but that always eventually got messed up. I finally decided that I wanted to sort the lines properly (after organizing the list into tables instead of nested lists.)

I’m pleased with what I came up with. First I made a tool called book-sorter, which prepends each line with a normalized version; here it is:


use strict;
use warnings;

no warnings 'uninitialized';

use feature 'fc';

while (<STDIN>) {
   my $v = $_;
   $v =~ s/^\W+//;
   $v =~ s/^(the|a|an)\b//i;
   $v =~ s/^\W+//;
   $v = fc $v;

   chomp $v;
   print "$v\0$_"

So this transforms each line in a streaming fashion, stripping non-word characters, then articles (a, an, or the), and finally any more non-word characters. Finally it case-folds the string so comparisons will be case-insensitive. I could have lowercased, but this will work better with non-ascii.

Next I build a script called book-sort that uses book-sorter:


bin/book-sorter |
   sort |
   cut -d "$(printf "\0")" -f2

Pretty neat!

If you’re interested in diving deeper than is probably wise in writing shell scripts, you should check out From Bash to Z Shell. The book has in depth coverage of all of the major POSIX shells and their non-POSIX features.

On a completely different note, I recently read Spy the Lie, suggested by Jess Frazelle. It was fascinating! I am a little nervous to put any such skills to use, but it’s better to know if someone is lying than to incorrectly assume they are being honest.

Posted Thu, Mar 21, 2019

If you're interested in being notified when new posts are published, you can subscribe here; you'll get an email once a week at the most.