Skip to content

Instantly share code, notes, and snippets.

@sidor1989
Created March 22, 2019 14:23
Show Gist options
  • Save sidor1989/46f347d18a4d162d5c714011701eb4ab to your computer and use it in GitHub Desktop.
Save sidor1989/46f347d18a4d162d5c714011701eb4ab to your computer and use it in GitHub Desktop.
// BEGIN
$app->get('/', function ($params) use ($data) {
if (array_key_exists('sort', $params)) {
list($key, $order) = explode(' ', $params['sort']);
usort($data, function ($prev, $next) use ($key, $order) {
$prevValue = $prev[$key];
$nextValue = $next[$key];
if ($prevValue == $nextValue) {
return 0;
}
if ($order == 'desc') {
return $prevValue < $nextValue ? 1 : -1;
} else if ($order == 'asc') {
return $prevValue > $nextValue ? 1 : -1;
}
});
}
return json_encode($data);
});
// END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment