title: DynamoDb retrieve data Order by descending order and then use pagination like sql syntax
body: <p>I am facing problem to retrieve records in descending order with pagination limit from amazon dynamodb as in mysql. 
  Now I am using the following script, but it gives unordered list of records. I need the last inserted id is on top.</p>
  
  <pre><code>$limit      =   10;
  $total = 0;
  $start_key = null;
  $params = array('TableName' =&gt; 'event','AttributesToGet' =&gt;array('id','interactiondate','repname','totalamount','fooding','nonfooding','pdfdocument','isMultiple','payment_mode','interaction_type','products','programTitle','venue','workstepId','foodingOther','interaction_type_other'), 'ScanFilter'=&gt; array('manufacturername' =&gt; array("ComparisonOperator" =&gt; "EQ", "AttributeValueList" =&gt; array(array("S" =&gt; "$manufacturername")))),'Limit'=&gt;$limit );
  $itemsArray = array();
  $itemsArray = array();
  $finalItemsArray = array(); 
  $finalCRMRecords = array();
  do{
  if(!empty($start_key)){             
  $params['ExclusiveStartKey'] = $start_key-&gt;getArrayCopy();
  }
  $response = $this-&gt;Amazon-&gt;Dynamodb-&gt;scan($params);     
  if ($response-&gt;status == 200) {
  $counter = (string) $response-&gt;body-&gt;Count;
  $total += $counter;
  foreach($response-&gt;body-&gt;Items as $itemsArray){
  $finalItemsArray[]  =   $itemsArray;    
  }
  if($total&gt;$limit){
  $i =1;
  foreach($response-&gt;body-&gt;Items as $items){
  $finalItemsArray[]      =   $items;
  if($i == $limit){
  $start_key = $items-&gt;id-&gt;{AmazonDynamoDB::TYPE_NUMBER}-&gt;to_array();
  $finalCRMRecords['data']    =   $finalItemsArray;
  $finalCRMRecords['start_key']   =   $start_key;
  break;
  }
  $i++;
  }
  }elseif($total&lt;$limit){
  $start_key = $response-&gt;body-&gt;LastEvaluatedKey-&gt;to_array();
  }else{
  $finalCRMRecords['data']    =   $finalItemsArray;
  if ($response-&gt;body-&gt;LastEvaluatedKey) {
  $start_key =$response-&gt;body-&gt;LastEvaluatedKey-&gt;to_array();
  break;
  } else {
  $start_key = null;
  }
  $finalCRMRecords['start_key']   =   $start_key; 
  }
  }       
  }while($start_key);
  </code></pre>
  
  <p>Regards</p>
  
  <p>Sandeep Kumar Sinha</p>