{"id":330,"date":"2011-11-02T15:06:13","date_gmt":"2011-11-02T19:06:13","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=330"},"modified":"2011-12-26T12:15:02","modified_gmt":"2011-12-26T17:15:02","slug":"the-emacs-sort-columns-command","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=330","title":{"rendered":"The Emacs <code>sort-columns<\/code> Command"},"content":{"rendered":"\n<p>Xah Lee has caught the VimGolf with Emacs bug and proposed an <a href=\"http:\/\/xahlee.blogspot.com\/2011\/10\/emacs-golf-align-and-sort.html\">interesting challenge<\/a>. The final part of the solution involved sorting the following list by the number of square kilometers. <\/p>\n<pre class=\"example\">California             423,970 km\u00b2\r\nTaiwan                  36,008 km\u00b2\r\nJapan                  377,944 km\u00b2\r\nGermany                357,021 km\u00b2\r\nIraq                   438,317 km\u00b2\r\nIran                 1,648,195 km\u00b2\r\nKorea (North+South)    219,140 km\u00b2\r\nMexico               1,964,375 km\u00b2\r\n<\/pre>\n<p> Normally this wouldn&#8217;t be much of a problem but two things make it more difficult than usual. First, the line for Korea has an extra field so commands like <code>sort-fields<\/code> or <code>sort-numeric-fields<\/code> don&#8217;t work. Second, the numbers have commas in them so the numeric sorts don&#8217;t work. <\/p>\n<p> I <a href=\"http:\/\/xahlee.blogspot.com\/2011\/10\/emacs-golf-align-and-sort.html?showComment=1320142276138#c2395595359271381932\">remarked<\/a> on Xah&#8217;s blog that I had tried <code>sort-regexp-fields<\/code> but that that didn&#8217;t work because the sort was lexicographic rather than numeric. Then <a href=\"http:\/\/xahlee.blogspot.com\/2011\/10\/emacs-golf-align-and-sort.html?showComment=1320149158613#c5526471563646671675\">jm<\/a> commented that the lexicographic sort will work if you include the spaces in front of the shorter numbers. He proposed using <code>sort-regexp-fields<\/code> with the regular expression <code>.*\\([0-9 ,]\\{9\\}\\) km<\/code>. That did the trick and I thought it was probably as simple a solution as we were apt to find. <\/p>\n<p> Later I suddenly realized that an even simpler solution based on the same idea was to use <code>sort-columns<\/code>. To use that function, you mark the rectangle containing the columns you want to sort on and then just call <code>sort-columns<\/code>. Thus, I placed the point two spaces before 423,970 on the first line, typed \u3010<span class=\"key\">Ctrl<\/span>+<span class=\"key\">Space<\/span>\u3011 to set the mark, moved the point to right after 1,964,375 on the last line, and typed \u3010<span class=\"key\">Meta<\/span>+<span class=\"key\">x<\/span>\u3011 <code>sort-columns<\/code> to sort the list. That worked perfectly and is a reasonably concise solution. <\/p>\n<p> As usual, Emacs had the right tool to get the job done even if it took a little effort to find it. Maybe next time this solution will jump right into my head. Or not. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xah Lee has caught the VimGolf with Emacs bug and proposed an interesting challenge. The final part of the solution involved sorting the following list by the number of square kilometers. California 423,970 km\u00b2 Taiwan 36,008 km\u00b2 Japan 377,944 km\u00b2 &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=330\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[18],"class_list":["post-330","post","type-post","status-publish","format-standard","hentry","category-general","tag-emacs"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=330"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":428,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions\/428"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}