# Sort on timestamp returns wrong order

**URL:** https://discuss.elastic.co/t/sort-on-timestamp-returns-wrong-order/76201
**Category:** Elasticsearch
**Created:** [February 23, 2017, 11:25am UTC](https://discuss.elastic.co/t/sort-on-timestamp-returns-wrong-order/76201 "2017-02-23T11:25:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mapkyca](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mapkyca/32/15796_2.png) [@mapkyca](https://discuss.elastic.co/u/mapkyca)
#### Post date: [February 23, 2017, 11:25am UTC](https://discuss.elastic.co/t/sort-on-timestamp-returns-wrong-order/76201/1 "2017-02-23T11:25:20Z")

</div>

Pulling my hair out on this one...

Using ES 2.3

Trying to sort results based on a unix epoch field, but results are being returned in the wrong order... however, they _are_ being sorted (you can reverse the order and get the incorrect results in the reverse order)

E.g, search returns three results

last\_action: 1472228572 (181 days ago)  
last\_action: 1471424370 (190 days ago)  
last\_action: 1473689679 (164 days ago)

Changing the order to ascending we get

last\_action: 1473689679 (164 days ago)  
last\_action: 1471424370 (190 days ago)  
last\_action: 1472228572 (181 days ago)

Sort is:

$query['sort'] = [  
'last\_action' =\> [  
'order' =\> 'desc',  
],  
];

Mapping for this field is:

'last\_action' =\> [  
'type' =\> 'date',  
'format' =\> 'epoch\_second',  
'index' =\> 'analyzed',  
'store' =\> 'yes'  
]

Any idea what could be throwing this off?

There are no exceptions being thrown by the server during storage or mapping...

Marcus

---

<div class="post-metadata">

### Author: ![jkuang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jkuang/32/72637_2.png) [@jkuang](https://discuss.elastic.co/u/jkuang)
#### Post date: [February 23, 2017, 11:30pm UTC](https://discuss.elastic.co/t/sort-on-timestamp-returns-wrong-order/76201/2 "2017-02-23T23:30:29Z")

</div>

I'm not able to reproduce your issue. Also, I'm not sure where you're getting some of the [] syntax from. I'm running Sense from Kibana.

This is what I did:

```auto
PUT mypayload
{
    "mappings":{
      "payload_type" : { 
        "properties": {
                  "last_action" : {
"type" : "date",
"format" : "epoch_second",
"index" : "analyzed",
"store" : "yes"
}
        }
}
}
}

```

Ran the post several times for all your time frames.

```auto
POST mypayload/payload_type
{
  "last_action" : 1472228572
}

```

Then get the output

```auto
GET mypayload/payload_type/_search
{ 
  "sort": [
    {
      "last_action": {
        "order": "desc"
      }
    }
  ]
  }

```

Output:

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": null,
    "hits": [
      {
        "_index": "mypayload",
        "_type": "payload_type",
        "_id": "AVptPpHf-G5KikZv-qhL",
        "_score": null,
        "_source": {
          "last_action": 1487891449
        },
        "sort": [
          1487891449000
        ]
      },
      {
        "_index": "mypayload",
        "_type": "payload_type",
        "_id": "AVptPsb0-G5KikZv-qhM",
        "_score": null,
        "_source": {
          "last_action": 1473689679
        },
        "sort": [
          1473689679000
        ]
      },
      {
        "_index": "mypayload",
        "_type": "payload_type",
        "_id": "AVptP2A4-G5KikZv-qhO",
        "_score": null,
        "_source": {
          "last_action": 1472228572
        },
        "sort": [
          1472228572000
        ]
      },
      {
        "_index": "mypayload",
        "_type": "payload_type",
        "_id": "AVptPyUY-G5KikZv-qhN",
        "_score": null,
        "_source": {
          "last_action": 1471424370
        },
        "sort": [
          1471424370000
        ]
      }
    ]
  }
}

```

---

<div class="post-metadata">

### Author: ![mapkyca](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mapkyca/32/15796_2.png) [@mapkyca](https://discuss.elastic.co/u/mapkyca)
#### Post date: [February 24, 2017, 9:37am UTC](https://discuss.elastic.co/t/sort-on-timestamp-returns-wrong-order/76201/3 "2017-02-24T09:37:42Z")

</div>

There appears to be no way to close this thread, but I've managed to get to the bottom of this - in summary it was a bug in a third party library, and not elastic search.

I came back to it fresh and found the issue in about 15 minutes. Always the way 😕

Thanks for your help!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 24, 2017, 9:37am UTC](https://discuss.elastic.co/t/sort-on-timestamp-returns-wrong-order/76201/4 "2017-03-24T09:37:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
