title: DynamoDB Sort and select top 20 records
body: <p>I wanted to get records like top 10 or 20 basically some configurable number which are sorted based on a date field stored as ISO-8601 format <code>uuuu-MM-dd</code> after applying some filters.</p>
  
  <pre><code>@DynamoDBTable(tableName = "Data")
  @NoArgsConstructor
  @ToString
  public class Data {
  
      private LocalDate date;
      @Setter
      private Long number;
      @Setter
      private STATUS status;
  
      @DynamoDBHashKey
      @DynamoDBTypeConverted(converter = LocalDateConverter.class)
      public LocalDate getDate() {
          return date;
      }
  
      @DynamoDBTypeConverted(converter = LocalDateConverter.class)
      public void setDate(LocalDate date) {
          this.date = date;
      }
  
      @DynamoDBIndexHashKey(globalSecondaryIndexName = "idx_Data_number")
      public Long getCompetitionId() {
          return number;
      }
  
      @DynamoDBTypeConvertedEnum
      @DynamoDBIndexHashKey(globalSecondaryIndexName = "idx_Data_status")
      public STATUS getStatus() {
          return status;
      }
  }
  </code></pre>
  
  <p>I need to get say 20 records sorted based on date field after filtering based on number and multiple status. I was using <code>ScanRequest</code> to basically filter the records. Now I want to limit the results and get top records based on the date.</p>
  
  <pre><code>@AllArgsConstructor
  public class DataRepositoryImpl implements DataRepository {
   private final DynamoDBMapper dynamoDBMapper;
   private final AmazonDynamoDB amazonDynamoDB;
  
   @Override
   public List&lt;Data&gt; findAll(Long number, List&lt;String&gt; status) {
      Condition numberCondition = new Condition()
              .withComparisonOperator(ComparisonOperator.EQ)
              .withAttributeValueList(new AttributeValue().withN(String.valueOf(number)));
  
      List&lt;AttributeValue&gt; attributeValues = newArrayList();
      status.forEach(s -&gt; attributeValues.add(new AttributeValue().withS(s)));
      Condition statusCondition = new Condition()
              .withComparisonOperator(ComparisonOperator.IN)
              .withAttributeValueList(attributeValues);
  
      Map&lt;String, Condition&gt; conditions = newHashMap();
      conditions.put("number", numberCondition);
      conditions.put("status", statusCondition);
  
      ScanRequest scanRequest = new ScanRequest()
              .withTableName("Data")
              .withScanFilter(conditions)
              .withConditionalOperator(ConditionalOperator.AND);
  
      ScanResult scanResult = amazonDynamoDB.scan(scanRequest);
      return dynamoDBMapper.marshallIntoObjects(Data.class, scanResult.getItems());
      }
  }
  </code></pre>