简体   繁体   中英

Linux Sort in wrong order

I've a problem using Linux Sort when i run on input like this:

07TLF\1\1\10040
07TLF\1\1\10060
07TLF03\10060
07TLF03\10080
07TLF\1\1\10320
07TLF\1\1\10360
07TLFS\10280

(\\1 is Ctrl+A character). As you can see the 3rd and 4th row are wrong (should be the 5th and 6th). If I use LC_COLLATE=C everything goes well! but in other similar cases sort works ok even if I use a different LC_COLLATE.

I use sort in a C program using system system call, but also running in bash I have the same behavier.

Can you help me? thx

From the manpage of sort (1):

NAME
       sort - sort lines of text files

I don't think, Ctrl+A is part of any file called "text file". If you have special requirements - as sorting special chars - you have to either use a utility serving the requirement or just implement it on your own if no such exists.

Since there is no "natural order" of special chars, you will hafe do define what you want the order to be. You may either set the locale to make sort behave according to the rules defined there or you have to implement your own sort algorithm, if none of the locales matches your requirements.

By the way: Using system is not a good idea at all if you can avoid it. You may never know which version of sort is installed on your target system and which rules are used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM