Thoughts on Perl and Emacs, technology and writing

Sorting Records With Emacs

I use sort-lines fairly frequently to keep my #includes, uses or variable names in order. Occasionally though, I need to sort something slightly more complicated and I end up writing a little perl script. I found myself thinking it must be equally easy in emacs.

For example, I say I have the following data which I want to sort by the second field which is in position 12 and 13.

Now, emacs does have a sort-columns function which does all sorts of fancy things including checking if we are using Unix and shelling out to sort if possible, but it doesn’t seem to be able to do a numerical comparison. However, in sort.el it does have a general purpose sorting function called sort-subr.

@Aaron – I think that sort-numeric-fields counts fields based on whitespace, therefore for fixed width fields with a variable number of words it won’t work (e.g. my name field will sometimes have two surnames). Good tip for other data though.

@Sue – sort-subr is even more flexible isn’t it? Pathological example: what if you wanted to sort on the deepest set of brackets?

XXX ((1)) (2)

@Eisen – portability and flexibility. sort doesn’t exist by default on Windows and can’t handle all my sorting needs in any case.

Hello my name is Pedro, i’am from Portugal. I’am in the firs year of university and i have to do a program in scheme call’d mastermind. do you have some ideas to the program? i need help! if you can’t it’s ok but i don’t have more options. i find this blog and i think maybe he can help but if you din’t it’s ok! my e-mail is there if you can help send me an e-mail…. Thanks by the way… nice post…