I need to sort an array of strings, but I need it so that null is always last. For example, the array:
var arr = [a, b, null, d, null]
When
I am sorting objects with a custom index and this works for me. I am not wanting to change the original array and it is important to keep the null indexes where they are.
let sorted = [...array].sort((a, b) => {
if (!a || !b) return 0;
else return a.CustomIndex - b.CustomIndex;
});
Do it like:
var arr = [a, b, null, d, null]
foreach ($arr as $key => $value) {
if($value == null)
unset($arr[$key]);
$arr[] = $value;
}
// rebuild array index
$arr = array_values($arr);
echo '<pre>';print_r($arr);die;