Domanda

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?
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top