couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Davis <>
Subject Re: View collation
Date Tue, 31 Mar 2009 23:03:29 GMT

This is how JSON is sorted by type. For reference the current version
looks like this:

type_sort(V) when is_atom(V) -> 0;
type_sort(V) when is_integer(V) -> 1;
type_sort(V) when is_float(V) -> 1;
type_sort(V) when is_binary(V) -> 2;
type_sort(V) when is_list(V) -> 3;
type_sort({V}) when is_list(V) -> 4;
type_sort(V) when is_tuple(V) -> 5.

This means, that all atoms (null, true, false) sort first, then
integers, floats, binaries (strings), lists (arrays), and then {V}
when is_list(V) (objects). I'm pretty sure that the last one with V
when is_tuple(V) is for making sure couchdb internal structures get
sorted last because I don't think its possible for an Erlang
representation of JSON to get past the previous line.

The {obj, _} code you see is the old version of {V} when is_list(V)
because of the changes in the Erlang representation of JSON since 0.8.
For reference, {obj, [{<<"key">>, 1}]} became {[{<<"key">>, 1}]} which
represents {"key": 1}

Paul Davis

On Tue, Mar 31, 2009 at 6:50 PM, kowsik <> wrote:
> I'm working on something that hopefully will help couch newbies. Will
> share once it's ready. In the meantime I have a question on view
> collation. Looking at couch_view.erl I see the following line:
> type_sort({obj, _}) -> 4; % must come before tuple test below
> OTOH, doesn't have a
> corresponding entry for this. Since I can't grok erlang just yet, what
> does this do? From the looks of it any key that matches this clause
> sorts between an array and an object. But what does that key look
> like? Some JSON examples will be appreciated.
> Thanks,
> K.

View raw message