Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id B2BEAB5D803 for ; Tue, 14 Jun 2011 08:14:23 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 70442-07 for ; Tue, 14 Jun 2011 11:14:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from depesz.com (andy.depesz.com [88.198.47.100]) by mail.postgresql.org (Postfix) with ESMTP id 5C000B5D80B for ; Tue, 14 Jun 2011 08:14:17 -0300 (ADT) Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by depesz.com with esmtpa (Exim 4.76) (envelope-from ) id 1QWRZX-0003Cw-Ms; Tue, 14 Jun 2011 13:14:15 +0200 Date: Tue, 14 Jun 2011 13:14:15 +0200 From: hubert depesz lubaczewski To: Mike Hepworth Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6056: sorting issues Message-ID: <20110614111415.GA16856@depesz.com> Reply-To: depesz@depesz.com References: <201106132152.p5DLq6Hq046860@wwwmaster.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <201106132152.p5DLq6Hq046860@wwwmaster.postgresql.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/103 X-Sequence-Number: 30482 On Mon, Jun 13, 2011 at 09:52:06PM +0000, Mike Hepworth wrote: > I execute the following sql statment: > > select * from (values('HALL, ANDY'),('HALLBERG, FRANK'),('HALLDEN, > DOUGLAS'),('HALL, DOUGLAS') ) x ORDER BY 1; > > and get the following results > > HALL, ANDY > HALLBERG, FRANK > HALLDEN, DOUGLAS > HALL, DOUGLAS > Please Help... it would help if you would say what do you think is wrong with it. because, the data is well sorted - prefix hall, and then, the next sortable character is: A B D D in 3rd and 4th string 2nd character is also taken into consideration because 1st is the same, so it becomes: A B DE DO so the sort order is correct. if you'd expect to get output: > HALL, ANDY > HALL, DOUGLAS > HALLBERG, FRANK > HALLDEN, DOUGLAS then the proper solution is to use separate fields for lastname and firstname, and not put them both in the same column. In Pg 9.1 you will be able to sort it the way you want using COLLATE support. with current situation, you can try to split every string using ", ", and sort splitted elements separately. Best regards, depesz -- The best thing about modern society is how easy it is to avoid contact with it. http://depesz.com/