+ Start a Discussion
WarrenWarren 

Sorting query result in PHP 5

Has anyone managed to sort a query result array of sObjects in ascending order of CreatedDate (or any other SalesForce date field) in PHP? I have read a few posts containing these kinds of requests (using USort) but none seem to work with datetime fields.
 
Thanks in advance.
 
Warren Lester
Tran ManTran Man
Try this:

Code:
function DateCmp($a, $b) {
  $a_datetime = $a->fields->LastModifiedDate;
  $b_datetime = $b->fields->LastModifiedDate;
  if ($a_datetime == $b_datetime) {
    return 0;
  } else {
    return ($a_datetime < $b_datetime) ? -1 : 1;
  }
}
  
  ...

  $query = 'SELECT Name, LastModifiedDate from Account';
  $response = $mySforceConnection->query(($query));
  $records = $response->records;

  $sObjectsArray = array();
  foreach ($records as $ThisAccount) {
    $sObject = new SObject($ThisAccount);
    array_push($sObjectsArray, $sObject);
  }
  usort($sObjectsArray, 'DateCmp');

  foreach ($sObjectsArray as $object) {
    echo $object->fields->LastModifiedDate.'<BR />';
  }


Message Edited by Tran Man on 04-07-2006 10:43 AM