Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP Array Sort Example
$subject = [
  ['someValue' => 1],
  ['someValue' => 2],
  ['someValue' => 3],
];

usort($subject, 'desc');

// Ordenação crescente
function asc($current, $next)
{
  $current = $current['someValue'];
  $next = $next['someValue'];
  
  if ($current == $next) {
    return 0;
  }
  
  return ($current < $next) ? -1 : 1;
}

// Ordenação decrescente
function desc($current, $next)
{
  $current = $current['someValue'];
  $next = $next['someValue'];
  
  if ($current == $next) {
    return 1;
  }
  
  return ($current < $next) ? 1 : 0;
}

var_dump($subject);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.