Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort lines semi-lexiographically in emacs -- i.e., lexiographically, except that 3 gets sorted above 11?

Tags:

emacs

How do I sort lines semi-lexiographically in emacs -- i.e., lexiographically, except that 3 gets sorted above 11? For example, I have a large collection of data, each entry of which looks like

[ 5, 3, 21, 1600000 ],
[ 3, 11, 21, 6400000 ],
[ 3, 3, 102, 1600000 ],

etc...

M-x sort-lines sorts this as

[ 3, 11, 21, 6400000 ],
[ 3, 3, 102, 1600000 ],
[ 5, 3, 21, 1600000 ],

but I would really like this sorted as

[ 3, 3, 102, 1600000 ],
[ 3, 11, 21, 6400000 ],
[ 5, 3, 21, 1600000 ],

Thanks!

like image 704
mike Avatar asked Jun 23 '11 22:06

mike


2 Answers

sehe gives a good solution. Here it is in Emacs:

C-u M-| sort -k2n -k3n

Run that with your region selected and it will be replaced with the sort ouput!

like image 190
Brian McKenna Avatar answered Sep 23 '22 18:09

Brian McKenna


I don't use emacs, but in vim I'd do:

%!sort -k2n -k3n

(possibly using the other key columns as well, I can't tell form the sample)

I'm not starting the editor war here... I'm just pretty sure that emacs allows you to filter through a shell command as well, so this will help!

like image 30
sehe Avatar answered Sep 22 '22 18:09

sehe