SortWords in the EmacsWiki suggests this function as a way to sort words:
(defun sort-words (reverse beg end)
"Sort words in region alphabetically, in REVERSE if negative.
When prefixed with negative \\[universal-argument], sorts in
reverse.
The variable `sort-fold-case' determines whether alphabetic
case affects the sort order.
See also `sort-regexp-fields'."
(interactive "*P\nr")
(sort-regexp-fields reverse "\\w+" "\\&" beg end))
However, it does not behave sensibly on words with hyphens. Here is an example. Before:
a-magician pulled the-rabbit out-of the-hat
After using sort-words
(above):
a-hat magician of-out pulled-rabbit the-the
This is certainly interesting (in the Chinese proverb kind of way). I suspect it has to do with Destructive Sorting. (I get a lot of mileage from pointing out how strange that is!)
What is a reasonable way to improve sort-words
above?
Update: Would someone care to explain (perhaps in a comment or an answer) why the implementation above behaves strangely (at least to my eye) with the hyphens?