Re: Sorting composite types

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Miles Elam <miles(dot)elam(at)productops(dot)com>, PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Sorting composite types
Date: 2019-08-07 06:22:40
Message-ID: 52901b22c801808ae9f1e1f9edb7875269e1118a.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Miles Elam wrote:
> Is there any way to define a natural sorting order for composite types? For example, let's say you have a type like:
>
> CREATE TYPE contrived AS (
> i1 integer,
> i2 integer
> );
>
> The semantics of this contrived type are that the natural order is ascending NULLS first for i1 and descending NULLS last for i2.
> I know I can do an ORDER BY per query and specify each part separately, but if my contrived example has a perceived
> natural sort order, is there any way to set that in a global way so that I can simply and universally ORDER BY my_contrived_column?

You'd have to define a default b-tree operator class for the type with
operators that implement your desired sort order.

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Luca Ferrari 2019-08-07 07:15:35 Re: Why must AUTOCOMMIT be ON to do txn control in plpgsql procedure?
Previous Message Andy Colson 2019-08-07 01:01:54 Re: Guidance needed on an alternative take on common prefix SQL