Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jMCxX-0002o9-2B for pgsql-general@arkaria.postgresql.org; Wed, 08 Apr 2020 15:49:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jMCxV-0006NB-Cq for pgsql-general@arkaria.postgresql.org; Wed, 08 Apr 2020 15:49:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jMCxV-0006N3-5B for pgsql-general@lists.postgresql.org; Wed, 08 Apr 2020 15:49:45 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jMCxO-0000nd-8H for pgsql-general@postgresql.org; Wed, 08 Apr 2020 15:49:44 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 779354C2; Wed, 8 Apr 2020 11:49:36 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 08 Apr 2020 11:49:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=u alYuOI2cGQyy71kt9IkfbYRMrbjoyI7O7b97mmuAek=; b=Wfop/qK5rXyaNnhwM UKBK7KzgyPSXWt7zk60ZxTXpsAOEDXnsR9q1oHCNMLhdYPML2XBbCKbrq9c9qXdR Eiljk8dEbFfyYxCmDA+FDDwO+LzZa2+I/wGz9ak5bjMiAzgHF7weWpcXOfl3dBv3 M0cXkmG8lrFJLcQ+SuwRYzcFD3OiUIIWqa0fFJtOgUigO+SaiZgv3+JIvk3vqvrU FAhV3af0NUYVwPfTzryf5AqUkW0/Eul5k2nm2qKbhMdpzzbx+hvknqzGp7m8cob5 KOef60huMvqKvGSIn3WOZAMwnyfsRt9HGgFkdHkP/B18rBU8tWxwtw4BwkbVXr4d AIP7A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=ualYuOI2cGQyy71kt9IkfbYRMrbjoyI7O7b97mmuA ek=; b=buy40WHHitNfwygdyKXMxhw2TFEQJGtSlIk+lmYaUkY9Ne6+JxZT6s2kG 6a1W/XafrVDr1U6gms8XKaWcjhYU1PWx9ZqnQgXtXKrC+LdYTGSG69OCNfzOWPty 1n1PCTpgBs93SmS2KjA2qH/m4VwPL+ygXQQtqF6QLE3KAQN9L/Djobyx35LUwc+H FHLvoe8QpvFpKpCQyLbO8GsIBV/703ibfw00JeRp1buuqtXiByh+21pDDIQOvMD6 Fl8r9KbWSzxFgqVi+jCgUbvnJ4bXQ0PEF0UFlJCufClrV09AxPwxY+/iYOF37LVX V8SCd7+ASU34SCNlBluLOb1lRTqyw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrudejgdelvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjeehrddujedv rdeltddrledvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (75-172-90-92.tukw.qwest.net [75.172.90.92]) by mail.messagingengine.com (Postfix) with ESMTPA id 34AA0328005D; Wed, 8 Apr 2020 11:49:35 -0400 (EDT) Subject: Re: Unexpected behavior sorting strings To: Jimmy Thrasher , pgsql-general@postgresql.org References: <410dddd6-bc66-48ca-ab9d-13889d94b62c@www.fastmail.com> From: Adrian Klaver Message-ID: Date: Wed, 8 Apr 2020 08:49:34 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <410dddd6-bc66-48ca-ab9d-13889d94b62c@www.fastmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/8/20 7:35 AM, Jimmy Thrasher wrote: > I'm seeing some unexpected behavior when sorting some strings, and it indicates I don't fully understand how postgresql string sorting works. > > As I understand it, postgresql sorts strings roughly like strcmp does: character by character based on encoding value. > > In particular, I'm seeing the following. I would expect "< S" to come first, because "<" (0x3c) is less than ">" (0x3e). > > ``` > supercatdev=# select unnest(array['> N', '< S']) as s order by s; > s > ----- > > N > < S > (2 rows) > ``` > > I've broken this down further: > ``` > supercatdev=# select '> N' < '< S'; > ?column? > ---------- > t > (1 row) > ``` > > Am I missing something about how sorting works? I believe you are looking for 'C' collation: test=# select unnest(array[('> N' collate "C") , ('< S' COLLATE "C")]) as s order by s; s ----- < S > N (2 rows) For more information see: https://www.postgresql.org/docs/12/collation.html > > Metadata: > - postgresql 9.5.19, running on Ubuntu 16LTS > - encoding, collate, and ctype are all UTF8 or en_US.UTF-8, as appropriate > > Thanks! > > Jimmy > > -- Adrian Klaver adrian.klaver@aklaver.com