Sorting with Regular Expressions

Tory Anderson has a nice use case for the sort-regexp-fields command. When I first saw his post I thought sort-regexp-fields was another new find for me. But on checking, I discovered that I had used in back in 2011 in an attempted solution to one of Xah Lee’s EmacsGolf challenges. Lee’s post has since disappeared but you can see the problem at the above link. Apparently sort-regexp-fields is one of those commands that I don’t use very often and tend to forget.

That’s too bad because, as Anderson shows, it can be very useful. It’s not even hard to use. You just need to remember that it exists. The basic idea is that you give the command two regular expressions. The first specifies the record—a line or paragraph, say—and the second matches the key in the record. Then the command sorts the records lexigraphically based on the key. That’s all there is to it. Simple but powerful.

This entry was posted in General and tagged . Bookmark the permalink.