Return-Path: Delivered-To: apmail-couchdb-dev-archive@www.apache.org Received: (qmail 27678 invoked from network); 15 Dec 2009 10:12:25 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Dec 2009 10:12:25 -0000 Received: (qmail 15188 invoked by uid 500); 15 Dec 2009 10:12:25 -0000 Delivered-To: apmail-couchdb-dev-archive@couchdb.apache.org Received: (qmail 15104 invoked by uid 500); 15 Dec 2009 10:12:24 -0000 Mailing-List: contact dev-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list dev@couchdb.apache.org Received: (qmail 15078 invoked by uid 99); 15 Dec 2009 10:12:24 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Dec 2009 10:12:24 +0000 X-ASF-Spam-Status: No, hits=1.2 required=10.0 tests=SPF_HELO_PASS,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [80.67.31.41] (HELO smtprelay03.ispgateway.de) (80.67.31.41) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Dec 2009 10:12:14 +0000 Received: from [77.3.102.234] (helo=[192.168.1.38]) by smtprelay03.ispgateway.de with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.68) (envelope-from ) id 1NKUNm-0000Sn-0c for dev@couchdb.apache.org; Tue, 15 Dec 2009 11:11:54 +0100 From: Kosta Welke Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: CouchDB book: Ordering Lists Date: Tue, 15 Dec 2009 11:11:52 +0100 Message-Id: <848E6B2B-7F98-4E30-97B1-607A38BFF3A0@fillibach.de> To: dev@couchdb.apache.org Mime-Version: 1.0 (Apple Message framework v1077) X-Mailer: Apple Mail (2.1077) X-Df-Sender: kosta@fillibach.de X-Virus-Checked: Checked by ClamAV on apache.org Hi all, first of all, I'm not sure if this is the right list to post feedback on = the book. I didn't want to use the "comment" function, as my experience = (on other pages) is that I'll never know if anyone even read the = comment. On http://books.couchdb.org/relax/reference/recipes, "Ordering Lists": You propose to use floats to sort lists, which can be rearranged or = where an arbitrary amount of items can be inserted between any two = items. What about solving the issue by sorting strings? Strings can be as long = as they want without any floating point precision getting in the way. To insert in a list, insert in the middle of the existing 'boundaries'. = For inserting items at the beginning or end, the boundaries are = "a".charCodeAt(0)-1 (which is "`") and "z".CharCodeAt(0)+1 (which is = "{"). If you want, you can extend that to more printable characters or = even unprintable ones. So, for the first item, the middle of the boundaries "`" and "{" is "m" = (verify using String.fromCharCode(("`".charCodeAt(0) + = "{".charCodeAt(0))/2) { "title":"Remember the Milk", "sort_order": "m" } Now, we insert a second item afterwards between "m" and "{", which is = "t": {"title":"Call Fred", "sort_order": "t" } An item between these two would have sort_order "p". If we ever have the case of inserting between two consequtive = characters, like "q" and "r" (i.e. where (a+b)/2 =3D=3D min(a,b)), we = extend the sort_order of the new item by one character, so it's "qm". In general, when trying to find the middle between two strings, we find = the first occurence where the strings differ. If you can squeeze = something in between, do it. If not, try to squeeze in the middle = between the next character of the first string and "{". If that's not = possible, try to squeeze in the middle between "`" and the next = character of the second string. If that's not possible, extend the first = string by "m". E.g. you want to insert between "qmbz" and "qmca". They share the prefix = "qm", the first difference is the third character, which is "b" and "c" = respectively. We cant squeeze in something between b and c, so we try to = squeeze something between the next character "z" and "{", which is not = possible. We also cannot squeeze anything between the next character = from the second string, which is "a", and "`". So our best bet is to = extend the first string by "m". The result is "qmbzm". If the first string would have been "qmby", the result would have been = "qmbz". If the first string would have been "qmbz" and the second = "qmcb", the result would have been "qmca". What do you think? It's a little more complicated than calculating the = average of two floats, but the complicatedness stems from fixing the = issue of limited precision. Of course, other ways of achieving something similar is to use an array = of integers (best would be int32 so that JavaScript can handle).=20 Cheers, Kosta=