Hamer
Hamer

Reputation: 1383

Azure Cosmos DB - order by a version string

Currently I have a list of documents in a collection. Each document has a version string field which I would like to order by. Although this example query does not work, I'm looking for something similar to the following. select * from p order by p.version desc I believe this may be possible using a stored procedure but is that the most efficient way if it is possible?

{
  "productId": "...",
  "version": "3.6.2",
  "platform": "..."
},
{
  "productId": "...",
  "version": "2.2.1",
  "platform": "..."
},
{
  "productId": "...",
  "version": "7.4.8",
  "platform": "..."
},
{
  "productId": "...",
  "version": "2.2.6",
  "platform": "..."
}

Upvotes: 1

Views: 1049

Answers (1)

Jay Gong
Jay Gong

Reputation: 23782

I'd like to sort based on the individual components of the version rather than the version string as a whole.

Basically, we are told we can only sort with properties of document, not derived values.So,you can't sort data like order by substring(version,0,1).

2 ways as workaround:

1.Follow @Martion's suggestion: Split version according to . into three properties and add them into your documents so that you could sort them as normal.

2.Select three derived properties in sql:

SELECT substring(c.version,0,1) as major,substring(c.version,2,1) as minor,substring(c.version,4,1) as patch FROM c

Then sort the result in the code,like Array.Sort().

Upvotes: 2

Related Questions