tags:

views:

203

answers:

2

I have these lines in Vim:

a
c
b
e
é
f
g

and when I do :%sort, I get this:

a
b
c
e
f
g
é

Obviously, the "é" line should not be at the end, it should be after the "e" line. Is it possible to get Vim to sort these lines correctly? Not using the ASCCI key for the characters but the actual character.

I also tried with :!sort (to use GNU sort utiliy) but I get the same result.

A: 

I get the following using :%!sort:

a
b
c
e
é
f
g

which is what is supposed to happen. It seems like your locale set up might be to blame. I'm not sure what exactly is wrong, but FWIW, I have the following environment setting: LANG=en_GB.UTF-8

Tomislav Nakic-Alfirevic
Same here. I'm on Ubuntu 9.04 with `sort` 6.10.
Benjamin Oakes
That’s weird, I have `LANG=fr_CA.UTF-8` and `LC_ALL=fr_CA.UTF-8`.
remi
+2  A: 

:%sort and :%!sort do not necessarily work in the same way. To quote :help sort:

The details about sorting depend on the library function used. There is no guarantee that sorting is "stable" or obeys the current locale. You will have to try it out.

On the other hand, GNU sort sorts according to the current locale. To quote man sort:

* WARNING * The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.

On my system (Ubuntu 9.10 with fr_CA.UTF-8 temporarily set) :%sort sorts as if C or POSIX was set, while :%!sort sorts according to the French locale.

My guess is that you've initially tried both :%sort and :%!sort under a POSIX-like locale (which yielded the same result), and then continued your experiments with different locales using :%sort only (which always returned POSIX-like order). Can you confirm that?

Bolo
Thanks for the clarification between `:%sort` and `:%!sort` in terms of locale. However, even with `LC_ALL` set to `fr_CA.UTF-8`, the `:%!sort` command still incorrectly sort the lines. The problem is probably not related to Vim, as running `sort` on a file in a regular terminal doesn't work correctly either. I'm on a Mac so that might be the problem. I'll look into that.
remi
That's strange... You're right that it's better to focus on GNU sort first, as the problem doesn't look to be vim-specific. I don't have access to a Mac right now, so I can't help you, but I've found this: http://discussions.info.apple.com/thread.jspa?messageID=11129073 Maybe that discussion is somehow relevant. Good luck!
Bolo