# Sort Array of objects by unix timestamp

**URL:** https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016
**Category:** General
**Created:** [22 October 2019 14:50 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016 "2019-10-22T14:50:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Rtholl](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Rtholl](https://discourse.nodered.org/u/Rtholl)
#### Post date: [22 October 2019 14:50 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016/1 "2019-10-22T14:50:38Z")

</div>

Hey guys,

I'm storing sensor data in a Cloudant DB. Every document consists of the 63 sensor values and a unix timestamp. When querying all the documents from the DB, I realized that those documents are not chronologically ordered.

Can anyone help me with a function to sort my array according to its timestamps?  
The array looks like this:

 ![27](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f9c6c02bba0959360bb54a2f00edb8e363453230.png)

The payload is an array containing all the documents of the DB. Each document is an object of Cloudant DB specific values (\_id and \_rev), "TS" = timestamp and "Values". "Values" is again an array of objects of the format {sensorID:temperature}.

My goal is to sort the array containing all documents according to their timestamps.

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [22 October 2019 15:06 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016/2 "2019-10-22T15:06:45Z")

</div>

Have you tried changing your db query to include “ORDER BY” ?

---

<div class="post-metadata">

### Author: ![Rtholl](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Rtholl](https://discourse.nodered.org/u/Rtholl)
#### Post date: [22 October 2019 17:11 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016/3 "2019-10-22T17:11:29Z")

</div>

I haven't seen that. I am not sure if you mean the SQL "ORDER BY"? Cloudant is a NoSQL DB and I use the built in Node-RED Node to connect and query my NoSQL DB

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [22 October 2019 17:37 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016/4 "2019-10-22T17:37:44Z")

</div>

I meant generically (i was on a train)

```auto
{
  "selector": {
    "date": {
      "$gt": 2010
    }
  }, 
  "fields": ["_id", "_rev", "insert_ts", "name"], //
  "sort": [{"insert_ts": "asc"}]
}

```

---

<div class="post-metadata">

### Author: ![Rtholl](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Rtholl](https://discourse.nodered.org/u/Rtholl)
#### Post date: [23 October 2019 13:34 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016/5 "2019-10-23T13:34:13Z")

</div>

This definitely makes sense. It would require tho that I launch the http request myself using the "http request" node instead of the built in cloudant node since there's no such option from the cloudant node directly.

The sort key has an array as value so I assume that I can set several sorting objects? I firstly want to sort all documents by their timestamp and then 2ndly within each document sort the Values according to a predefined csv file such that "Values array" sorts its objects according to the sensors position in real life. Any hint on how to solve the 2nd problem of sorting according to a predefined csv structure?

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [23 October 2019 17:49 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016/6 "2019-10-23T17:49:19Z")

</div>

There are several other cloudant nodes in [flows.nodered.org](http://flows.nodered.org) - I'm sure one will allow sorting.

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [23 October 2019 19:37 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016/7 "2019-10-23T19:37:00Z")

</div>

> [@Rtholl](#):
>
> My goal is to sort the array containing all documents according to their timestamps.

I agree that the sorting _should_ be done in the database, not after the fact -- but if you cannot find a way to have the database do it, you can always use the `sort` node:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e0cd77765e789a078bc7901561de259482743647.png)

---

<div class="post-metadata">

### Author: ![Rtholl](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Rtholl](https://discourse.nodered.org/u/Rtholl)
#### Post date: [19 December 2019 09:50 UTC](https://discourse.nodered.org/t/sort-array-of-objects-by-unix-timestamp/17016/8 "2019-12-19T09:50:39Z")

</div>

I tried and it works apart from the sorting. When I try to sort the output, it fails (see screenshots). I stick to all the introductions on the Cloudant documentation and follow your example. Do you see why I get a HTTP 400 back?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4386b0481d2c6ca2c88eb44a217c26e0327fe371.png)  
 ![image (1)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/c7499ad5ed294f2a6d0c72793bec60815deb6d09.png)
