Build, protect and deploy apps across any platform and mobile device
Leverage a complete UI toolbox for web, mobile and desktop development
Automate UI, load and performance testing for web, desktop and mobile
Rapidly develop, manage and deploy business apps, delivered as SaaS in the cloud
Build mobile apps for iOS, Android and Windows Phone
Optimize data integration with high-performance connectivity
Connect to any cloud or on-premise data source using a standard interface
Build engaging multi-channel web and digital experiences with intuitive web content management
Automate decision processes with a no-code business rules engine
Jeff Atwood touched an interesting topic yesterday, Sorting for Humans : Natural Sort Order.
Let's sort the following strings: a, A, b, B, 1, 2, 10
The ASCIIbetical order is as follows: 1, 10, 2, A, B, a, b
The natural sorting, for most human beings, is as follows: 1, 2, 10, a, A, b, B
Sorting strings in most programming languages will result in the ASCIIbetical result, and Jeff wonders if a more human-friendly natural sort option should be built into mainstream programming languages. What about XQuery? We're talking here about collations, and XQuery has built-in support for collations.
The default collation in XQuery is the Unicode Codepoint collation. For example,
[cc lang="xquery"]for $s in ("a", "A", "b", "B", "1", "2", "10")
order by $s
yields the following result: 1, 10, 2, A, B, a, b.
XQuery implementation are allowed to used a different default collation. With DataDirect XQuery, the default collation is based on the locale of your Java Virtual Machine. The query above will result in: 1, 10, 2, a, A, b, B. That's already better, 'a' and 'A' are sorted before 'b' and 'B'. By the way, using the locale implies that on a German system for example, characters like umlaut will collate as a German would expect.
But we're not yet there, the numbers are still not naturally sorted. You can achieve this with DataDirect XQuery, by explicitly overriding the default collation and specify the alphanumeric option. As shown in the next query,[cc lang="xquery"]declare default collation "http://www.datadirect.com/xquery/collation?alphanumeric=yes";
for $s in ("a", "A", "b", "B", "1", "2", "10")
order by $s
return $s[/cc] And we get the desired result: 1, 2, 10, a, A, b, B.
Want more tips and tricks?
digg_skin = 'compact'; digg_url = 'hhttp://www.xml-connection.com/2007/12/natural-sorting-in-xquery.html';
View all posts from Marc Van Cappellen on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Copyright © 2017, Progress Software Corporation and/or its subsidiaries or affiliates.
All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.