Skip to content

Instantly share code, notes, and snippets.

@Spea
Created May 24, 2018 16:25
Show Gist options
  • Save Spea/e6782d06bfd9e92dbbfc9b5f7d01d07e to your computer and use it in GitHub Desktop.
Save Spea/e6782d06bfd9e92dbbfc9b5f7d01d07e to your computer and use it in GitHub Desktop.
Comparison explanation
<?php
class NameValuePair
{
public $name;
public $value;
public function __construct($name, $value)
{
$this->name = $name;
$this->value = $value;
}
}
$aSortedPairs = [
new NameValuePair('l', 'l'),
new NameValuePair('b', 'b'),
new NameValuePair('k', 'k'),
new NameValuePair('c', 'c'),
new NameValuePair('o', 'o'),
new NameValuePair('e', 'e'),
new NameValuePair('m', 'm'),
new NameValuePair('q', 'q'),
new NameValuePair('f', 'f'),
new NameValuePair('g', 'g'),
new NameValuePair('p', 'p'),
new NameValuePair('d', 'd'),
new NameValuePair('h', 'h'),
new NameValuePair('i', 'i'),
new NameValuePair('j', 'j'),
new NameValuePair('s', 's'),
new NameValuePair('n', 'n'),
new NameValuePair('r', 'r'),
new NameValuePair('a', 'a'),
];
asort($aSortedPairs);
$aSortedNames = array_column($aSortedPairs, 'name');
$uaSortedPairs = [
new NameValuePair('l', 'l'),
new NameValuePair('b', 'b'),
new NameValuePair('k', 'k'),
new NameValuePair('c', 'c'),
new NameValuePair('o', 'o'),
new NameValuePair('e', 'e'),
new NameValuePair('m', 'm'),
new NameValuePair('q', 'q'),
new NameValuePair('f', 'f'),
new NameValuePair('g', 'g'),
new NameValuePair('p', 'p'),
new NameValuePair('d', 'd'),
new NameValuePair('h', 'h'),
new NameValuePair('i', 'i'),
new NameValuePair('j', 'j'),
new NameValuePair('s', 's'),
new NameValuePair('n', 'n'),
new NameValuePair('r', 'r'),
new NameValuePair('a', 'a'),
];
uasort($array, function ($first, $second) {
return $first <=> $second;
});
$uaSortedNames = array_column($uaSortedPairs, 'name');
var_dump($uaSortedNames === $aSortedNames); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment