Re: BUG #18564: Incorrect sorting with using NULLS LAST (NULLS FIRST) - Mailing list pgsql-bugs

From Daniel Verite
Subject Re: BUG #18564: Incorrect sorting with using NULLS LAST (NULLS FIRST)
Date
Msg-id 722b943c-42e7-4386-957a-312808f6e38c@manitou-mail.org
Whole thread Raw
In response to Re: BUG #18564: Incorrect sorting with using NULLS LAST (NULLS FIRST)  ("David G. Johnston" <david.g.johnston@gmail.com>)
List pgsql-bugs
    David G. Johnston wrote:

> In short, while this can be controlled at the SQL scope, the comparison
> operator for the json data type provides no such ability for the user to
> control the result of the comparison between null and non-null json
> values.

It looks like a good use case for NULLIF.

SELECT example_jsonb_column->'sorting_param'
FROM example_table
ORDER BY NULLIF(example_jsonb_column->'sorting_param', 'null'::jsonb)
  ASC NULLS LAST ;

The doc says that primitive JSON values are compared using the same
comparison rules as for the underlying PostgreSQL data type, so the
non-null values will be sorted as "numeric", which meets the OP's
expectations:

> Expected Behavior:
> NULL values should appear at the end of the sorted results, and the sorting
> should be accurate based on the numeric values.


Best regards,
--
Daniel Vérité
https://postgresql.verite.pro/
Twitter: @DanielVerite



pgsql-bugs by date:

Previous
From: "Ware, Christopher M. (LARC-D318)[RSES]"
Date:
Subject: RE: [EXTERNAL] Re: BUG #18528: Installer displays error when installing
Next
From: David Rowley
Date:
Subject: Re: BUG #18558: ALTER PUBLICATION fails with unhelpful error on attempt to use system column