{"id":63,"date":"2009-11-07T10:04:46","date_gmt":"2009-11-07T10:04:46","guid":{"rendered":"http:\/\/blog.soton.ac.uk\/webteam\/?p=63"},"modified":"2009-11-13T16:23:18","modified_gmt":"2009-11-13T16:23:18","slug":"the-1-10-2-3-problem","status":"publish","type":"post","link":"https:\/\/blog.soton.ac.uk\/webteam\/2009\/11\/07\/the-1-10-2-3-problem\/","title":{"rendered":"The 1, 10, 2, 3 problem."},"content":{"rendered":"<p>When you sort strings alphabetically, you end up with<\/p>\n<ul>\n<li>Project 1 Report<\/li>\n<li>Project 10 Report<\/li>\n<li>Project 2 Report<\/li>\n<li>Project 3 Report<\/li>\n<\/ul>\n<p>We&#8217;ve all seen this, right? Here&#8217;s an example in PERL:<br \/>\n<code>my @examples = (<br \/>\n\"Project 1 Report\",<br \/>\n\"Project 2 Report\",<br \/>\n\"Project 3 Report\",<br \/>\n\"Project 10 Report\",<br \/>\n\"Project 2342 Report\",<br \/>\n\"Alpha 2.9\",<br \/>\n\"Alpha 10.1\" );<\/p>\n<p>my @sorted = sort @examples;<br \/>\nprint join( \"\\n\",@sorted ).\"\\n\";<\/code><\/p>\n<p>This&#8217;ll give you:<br \/>\n<code>Alpha 10.1<br \/>\nAlpha 2.9<br \/>\nProject 1 Report<br \/>\nProject 10 Report<br \/>\nProject 2 Report<br \/>\nProject 2342 Report<br \/>\nProject 3 Report<\/code><br \/>\nWhat we really want is an alphabetic sort that treats numbers magically.<\/p>\n<p>Here&#8217;s a quick fix to make it do the thing you meant, by zero-padding any numbers before comparing the strings:<br \/>\n<code>my @sorted = sort sensible_sort @examples;<br \/>\nprint join( \"\\n\",@sorted ).\"\\n\";<\/p>\n<p>sub sensible_sort {<br \/>\n my $a1=$a;<br \/>\n my $b1=$b; # clone these so we don't modify originals<br \/>\n $a1 =~ s\/(\\d+)\/sprintf(\"%020d\",$1)\/ge;<br \/>\n $b1 =~ s\/(\\d+)\/sprintf(\"%020d\",$1)\/ge;<br \/>\n return $a1 cmp $b1;<br \/>\n}<\/code><\/p>\n<p>This modifies the strings used in the sort comparisons by finding every string of one or more digits 0-9 and replacing it with a 20 digit version, padded with zeroes. (20 digits is a number picked from my arse, 6 would probably do). This technique is easy enough to do in C or php or whatnot. There may well be a library out there which already does it, but it&#8217;s a neat self-contained little technique, which makes our lists from our learning objects repository far saner. Lecture 10 was listed second!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you sort strings alphabetically, you end up with Project 1 Report Project 10 Report Project 2 Report Project 3 Report We&#8217;ve all seen this, right? Here&#8217;s an example in PERL: my @examples = ( &#8220;Project 1 Report&#8221;, &#8220;Project 2 Report&#8221;, &#8220;Project 3 Report&#8221;, &#8220;Project 10 Report&#8221;, &#8220;Project 2342 Report&#8221;, &#8220;Alpha 2.9&#8221;, &#8220;Alpha 10.1&#8221; ); [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[137],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-sort"],"_links":{"self":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":5,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/63\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}