I have this data which i’m displaying in the table
[
{
"ID": 9,
"Status": "Pending",
},
{
"ID": 8,
"Status": null,
},
{
"ID": 7,
"Status": "Pending",
},
{
"ID": 10,
"Status": null,
},
{
"ID": 18,
"Status": "Completed",
},
{
"ID": 17,
"Status": "In Progress",
}
]
Sort Method:
Ascending order :
this.List.sort((a, b) => a[columnname] < b[columnname] ? 1 : a[columnname] > b[columnname]` ? -1 : 0);
Descending order :
this.List.sort((a, b) => a[columnname] > b[columnname] ? 1 : a[columnname] < b[columnname] ? -1 : 0);
and using sort function to sort the data in table issue is when i sort it, i want to place null values at the last. In current scenario they are in between other values
Like
Completed
null
In Progress
null
Pending
null
..
2
Answers
Couple of things for your sorting function
<
and>
to compare strings. It does weird things with upper/lower case, special characters, numbers, etc. there is aString.localeCompare
function that can be used to compare stringsnull
(or other special values) always at the end, just let it return a custom value that is always the sameThis is a sample sorting function, that contains a check for number & string and always sorts null values together
if you call
mySort(list, 'Status', false)
you will notice that all the null values are at the end while the rest is still properly sorted.If you want to have
null
in the end for both descending and ascending you can adapt the special=== null
check to something likeA fairly short way to do this would be:
In the snippet above we start with destructuring both comparator objects.
({ [columnName]: a }, { [columnName]: b })
stores the value of thecolumnName
property of the first argument in the variablea
andb
for the second argument.We then start with
(a == null) - (b == null)
to move thenull
values to the back of the array. This works becausetrue
coerces into1
andfalse
coerces into0
. So ifa
isnull
andb
is not, thentrue - false
will evaluate to1 - 0 //=> 1
. A positive return value means thata
is placed behindb
. If we are in the opposite scenario anda
is notnull
butb
is, thenfalse - true //=> -1
. A negative return value means thata
is placed beforeb
.Both
1
or-1
are truthy values, which cause the OR operator||
to short circuit and return the first operand. If both values arenull
or both values are notnull
, then we end up withtrue - true
orfalse - false
which both evaluate to0
. Since0
is a falsy value, we’ll move on to the second operand of the OR operator.This brings us to
-(a < b)
, which checks ifa
is smaller thanb
. When this is true-true
will evaluate into-1
, soa
is placed beforeb
. If this is false then-false
will evaluate into-0
, which is a falsy value and moves us to the next operand in the OR chain. Assume thata
andb
are bothnull
, thennull < null
isfalse
, thus moving us on to the next OR operand. Ifa
andb
are both notnull
, say the status string, then the strings are compared based on code point values and-1
is returned ifa
is smaller thanb
. Otherwise we move on to the final OR operand.+(a > b)
is very similar to-(a < b)
, but returns1
ifa
is greater thanb
. Placinga
afterb
in the result.Here are 6 example scenarios, showing the different outputs:
A less cryptic version could be written like so:
To apply an
asc
/desc
direction, but keep thenull
values at the back of the array, you can apply a modifier to the second part to inverse the sign (in case of a descending direction).Or: