Skip to content

Instantly share code, notes, and snippets.

@nibrahim
Created October 22, 2010 10:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nibrahim/640311 to your computer and use it in GitHub Desktop.
Save nibrahim/640311 to your computer and use it in GitHub Desktop.
Sort lines by length
(defun sort-lines-by-length (b e)
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region b e)
(let ((items (sort
(split-string
(buffer-substring (point-min) (point-max)) "[\n]")
(lambda(x y) (< (length x) (length y)))))
)
(delete-region (point-min) (point-max))
(save-excursion
(point-min)
(insert (apply 'concat (map 'list (lambda (x) (format "%s\n" x)) items))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment