title: perl custom sort by string similarity clustering
body: <p>In Perl, I would like to sort a collection of different length strings
  in a way that automatically lumps together similar strings.</p>
  
  <p>Intuitively, I imagine I need some distance measure for each pair and
  then a clustering routine that groups by the distance.</p>
  
  <p>My number of strings is always small and they are short, see an
  example below.</p>
  
  <p>Is there a simple approach that will do what I need in
  <code>sort_magic_here</code>?</p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  
  my @list =
    ("JK_HJ_Lancaster", "SY4_TS_HJ_1000ng",
     "NB_E_200cc_caHJ_Rep1", "HB_E_100cc_caHJ_Rep1",
     "HB_E_200cc_caHJ_Rep1", "Normal_Lancaster",
     "NB15_OP_HJ_1000ng","Zoey_HJ_Slough",
     "NB_E_100cc_caHJ_Rep1","Normal_Slough",
     "JK_caHJ_Slough","Zoey_HJ_Lancaster");
  
  print "# Straight sort\n";
  foreach my $elem (sort @list) {
    print "$elem\n";
  }
  
  print "# Sort grouped by string distance\n";
  foreach my $elem (sort { sort_magic_here() }  @list) {
    print "$elem\n";
  }
  </code></pre>