I have some PHP code which is doing a sort for me on my array of numbers. The numbers in particular (for example) are as followed:
“040613002625″,
“040613000277″,
“040613000511″,
“040613001685″,
“040613003275″, etc
When I sort my values however, 002625 is appearing before 001685 which is incorrect.
Note that I am using pagination on my page. I have 190 pages with listings of 30 items per page. The pagination could very well be the reason behind the issue as it’s not sorting all 5,000 listings and instead it’s only reading the first 30 and doing a sort on that single page. It’s also incredibly slow to read and render the page (upwards of 1 minute).
Here is a snippet of my existing code showing the sort logic. Does it look correct?
$per_page = 30;
$current_page = $this->get_pagenum();
$a = $this->users_data;
$this->users_data = array_slice($a, (($current_page - 1) * $per_page), $per_page);
// call the sorting function
usort($this->users_data, array(&$this, 'usort_reorder'));
// sorting function
function usort_reorder($a, $b)
{
// if no sort, default to user_login
$orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'ID';
// if no order, default to asc
$order = (!empty($_GET['order'])) ? $_GET['order'] : 'asc';
// determine sort order
$result = strcmp($a[$orderby], $b[$orderby]);
// send final sort direction to usort
return ($order === 'asc') ? $result : -$result;
}
Thanks.