Forked from adrianbadowski/Sort clothes and shoes sizes
Created
December 9, 2020 13:41
-
-
Save iksecreeet/0c5075286ce281a734c69cff86461fee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param mixed $a | |
* @param mixed $b | |
* | |
* @return int | |
*/ | |
protected function _sortSizes($a, $b) | |
{ | |
$aUpper = strtoupper($a); | |
$bUpper = strtoupper($b); | |
$sizes = array( | |
'XXXS', | |
'XXXS/XXS', | |
'XXS/XXXS', | |
'XXS', | |
'XXS/XS', | |
'XS/XXS', | |
'XS', | |
'XS/S', | |
'S/SX', | |
'S', | |
'S/M', | |
'M/S', | |
'M', | |
'M/L', | |
'L/M', | |
'L', | |
'XL/L', | |
'L/XL', | |
'XL', | |
'XL/XXL', | |
'XXL/XL', | |
'XXL', | |
'XXXL/XXL', | |
'XXL/XXXL', | |
'XXXL', | |
); | |
$asize = 100; | |
$apos = -1; | |
$bsize = 100; | |
$bpos = -1; | |
foreach ($sizes as $val => $str) { | |
$pos = ($aUpper == $str) ? 1 : -1; | |
if ($pos > 0 && ($apos < 0 || $pos < $apos)) { | |
$asize = $val; | |
$apos = $pos; | |
} | |
$pos = ($bUpper == $str) ? 1 : -1; | |
if ($pos > 0 && ($bpos < 0 || $pos < $bpos)) { | |
$bsize = $val; | |
$bpos = $pos; | |
} | |
} | |
if ($apos < 0) { | |
if (is_numeric($a) && is_numeric($b)) { | |
return ($a == $b) ? 0 : ($a > $b) ? 1 : -1; | |
} else { | |
return (is_numeric($a)) ? -1 : 1; | |
} | |
} | |
if ($bpos < 0) { | |
if (is_numeric($a) && is_numeric($b)) { | |
return ($a == $b) ? 0 : ($a > $b) ? 1 : -1; | |
} else { | |
return (is_numeric($b)) ? 1 : -1; | |
} | |
} | |
return ($asize == $bsize ? 0 : ($asize > $bsize ? 1 : -1)); | |
} | |
/** | |
* @param array $array | |
* | |
* @return array | |
*/ | |
public function sortSizes(array $array) | |
{ | |
usort( | |
$array, | |
array( | |
$this, | |
'_sortSizes', | |
) | |
); | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment