vividn
vividn

Reputation: 83

What sort order does couchdb use internally?

I'm unable to find a good answer of how couchdb determines it's sort order in views. I thought it would be in ASCII order or be similar to javascript's sort function which converts everything to UTF-16 strings and compares based off of their byte values. However, I was a bit shocked to find that with the following view:

(doc) => {
  emit("a",null)
  emit("b",null)
  emit("A",null)
  emit("B",null)
}

the keys are not sorted ["A","B","a","b"] as what Array.prototype.sort produces but rather ["a","A","b","B"]. What is the implementation of the sort order that couchdb uses?

Upvotes: 0

Views: 198

Answers (1)

RamblinRose
RamblinRose

Reputation: 4953

This is explained thoroughly in the CouchDB documentation 3.2.2.5. Collation Specification

Comparison of strings is done using ICU which implements the Unicode Collation Algorithm, giving a dictionary sorting of keys. This can give surprising results if you were expecting ASCII ordering.

Emphasis mine

A thorough read of that section should give anyone a solid grasp of the collation rules.

Upvotes: 1

Related Questions