Search code examples
amazon-web-servicesamazon-dynamodbaws-java-sdk

DynamoDB sort order of Date RangeKey


I have a DynamoDB table with the following key values: A simple string id as HashKey and a string representing a Date as RangeKey. The date string is in YYYY-MM-DD format.

I am now wondering how DynamoDB orders its entries. When I query for multiple RangeKey values on the same HashKey the result is ordered by the date ascending.

However, according to the Dynamo documentation it will order all non-integer RangeKeys considering their UTF-8 byte values.

When I now save the following RangeKey entries:

  1. 2019-01-01
  2. 2018-12-04
  3. 2018-12-05

The output of a simple DynamoDBMapper.query(...) results in the correct order:

  1. 2018-12-04
  2. 2018-12-05
  3. 2019-01-01

Is Dynamo ordering the RangeKeys by date or is the byte value calculated a way that it matches with the date representation?


Solution

  • Its sorting it in UTF-8 bytes. It has no idea that you are sorting dates, to DynamoDB, its just a string.