reddit / emacs

Sorting Text

atamariya · 2022-03-15 09:48
​ https://preview.redd.it/xmxg2cezqin81.png?width=1524&format=png&auto=webp&s=75156c1a65456e3f3ce0352a2914ff0789fb3368 TIL sorting text based on a column can be a piece of cake with Emacs [https://lifeofpenguin.blogspot.com/2022/03/sorting-text.html](https://lifeofpenguin.blogspot.com/2022/03/sorting-text.html)
trs_80 · 2022-03-15 20:00
Ha! I thought this might have been about you having the same problem as I was, with value of `case-fold-search` not being respected. Well, since I ended up looking it up anyway (to make sure I was remembering correctly), here is the function I wrote, in case anyone finds it useful: (defun my-sort-ignore-case (list &optional predicate) "Sort LIST using PREDICATE. PREDICATE defaults to `string<' (i.e., ascending alphabetical order). I wrote this when I realized that all forms of `sort' that I tried seem to disregard the value of the variable `case-fold-search' which (even by default!) is supposed to ignore case. In other words, strings are sorted with capitals first (in order), then lower case (in order), instead of mixing the upper and lower case together and sorting them all in order. This function fixes that. I also could not find a way to modify any locale settings (as I have experienced this affecting string sort order in the shell). This function works around that by making an alist out of the original word and a downcased version, then sorting on the downcased version, and finally returning the original words, now in order." (let ((pred (or predicate 'string<))) (mapcar (lambda (cons-cell) (cdr cons-cell)) (sort (mapcar (lambda (item) (cons (downcase item) item)) list) `(lambda (a b) (,pred (car a) (car b)))))))
atamariya · 2022-03-16 04:26
case-fold-search works at a lower level. At package level, it is mostly controlled by a package variable. e.g. for sort-columns, you need sort-fold-case for similar effect.
trs_80 · 2022-03-16 19:54
Cool, you gave me another lead to investigate. Cheers!