I have an array of objects in php, like:
array([0] => $obj1,
[1] => $obj2,
...)
where, $objN is an object of:
Class Student {
public $name;
public $fatherName;
public $dateOfBirth;
}
Now, I want to sort the above array as per the date of birth of students. I tried the following approach:
1- Creative a new associative array with $dateOfBirth as key, like:
array($dateOfBirth1 => $obj1,
$dateOfBirth2 => $obj2,
...)
2- Use ksort php function.
3- Re-convert this associative array into linear indexed array.
While this strategy works, there are potential flaws. Two students may have same date of births, in which case, only one will persist here, in array. Also, it's slightly computationally intensive to convert and reconvert arrays, to and from being associative.
Can someone suggest a better alternative?
PS: I am avoiding using any sorting algos like quick or merge sort.