[erlang-questions] sorting rules regarding bit strings

Ulf Wiger ulf.wiger@REDACTED
Thu Apr 2 16:36:29 CEST 2009


David Mercer wrote:
> On April 02, 2009, Ulf Wiger wrote:
> 
>> Maybe I'm just unusually dense today, but what are
>> the sorting properties of bit strings?
> . . .
>> Well, I think I can see the pattern, but is this really
>> what one wants?
> 
> The sort order seems right to me.  When I write out the bit
 > patterns it might make it clear:
> ...

Sure, and it's also consistent with how larger "words"
are sorted:

1> lists:sort([<<1:64>>,<<1:32>>,<<2:64>>]).
[<<0,0,0,0,0,0,0,1>>,
  <<0,0,0,0,0,0,0,2>>,
  <<0,0,0,1>>]

> Sorting is done bit-by-bit, with end-of-bitstring < 0 < 1.
> 
> Or am I just weird, thinking that that is intuitive?

I guess the main problem is that the way bitstrings
are pretty-printed gives the wrong visual cues,
as regards sorting.

<<1:1>> reads as the number one, and not necessarily
as "the most significant bit", whereas larger words,
as above give visual cues that explain the ordering
(just think of each element as a byte, and is sorts
just like a string.)

BR,
Ulf W
-- 
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com



More information about the erlang-questions mailing list