hung.dev
hung.dev

Reputation: 189

How to sort in array of object - CouchBase

Hi currently I want to sort array of object, I use ARRAY_SORT function, it will use the first field of object to sort & it work well if every object has the same JSON structure. If one element in array has different JSON structure, the result is incorrect.

The query I use :

SELECT ARRAY_SORT(c.student) as student FROM Class c

Result :

"student": [
  {
    "id": 3,
    "name": "Kenny35"
  },
  {
    "id": 6,
    "name": "Kenny35"
  },
  {
    "id": 7,
    "name": "Kenny35"
  },
  {
    "id": 8,
    "name": "Kenny35"
  },
  {
    "hobby": "video game",
    "id": 5,
    "name": "Kenny35"
  }
]

How can I specify property of object in array for ARRAY_SORT function ?

Upvotes: 1

Views: 793

Answers (2)

geraldss
geraldss

Reputation: 2445

You can issue a query and use ORDER BY.

SELECT *
FROM Class c
UNNEST c.student s
ORDER BY ...

Upvotes: 0

Prasad Varakur
Prasad Varakur

Reputation: 529

dev, Objects are first compared by length/size of the object, then followed by fields in the object. http://developer.couchbase.com/documentation/server/4.5/n1ql/n1ql-language-reference/comparisonops.html

That is the only collation supported now. -Prasad

Upvotes: 1

Related Questions