문제

The POSIX specification does not seem to say anything about what happens when one line provided to the sort program is the prefix of another.

But I find that my copy (GNU coreutils 8.4) gives precedence to the prefix:

$ echo -e 'foo\nfoobar' | sort
foo
foobar

$ echo -e 'foobar\nfoo' | sort
foo
foobar
  • Is this universal to all implementations of sort?
  • Where is this behaviour defined?
  • Can I rely on it?
  • Is this so because this is the universal definition of the word "sort" w.r.t. programming?
도움이 되었습니까?

해결책

Sort order is defined by the current locale, which is all the POSIX spec has to say. Your current locale (and most locales, I suspect) will consider a prefix to be lexicographically smaller than the string itself.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top