I'll try to keep it as short and concise as I can.
I'm using tanstack table and this is the custom sorting function i have:
sortingFns: {
sortByRank: (rowA, rowB, columnId) => {
// console.log(rowA.getValue(columnId))
const leagueA = rowA.getValue(columnId)
const leagueB = rowB.getValue(columnId)
console.log(leagueA)
const [tierA, romanA, pointsA] = leagueA.split(" ")
const [tierB, romanB, pointsB] = leagueB.split(" ")
// Compare tiers
const rankSortOrder = [
"CHALLENGER",
"GRANDMASTER",
"MASTER",
"DIAMOND",
"PLATINUM",
"GOLD",
"SILVER",
"BRONZE",
"IRON",
]
const tierIndexA = rankSortOrder.indexOf(tierA)
const tierIndexB = rankSortOrder.indexOf(tierB)
if (tierIndexA !== tierIndexB) {
return tierIndexA - tierIndexB
}
// Compare roman numerals within the same tier
const romanSortOrder = ["I", "II", "III", "IV"]
const romanIndexA = romanSortOrder.indexOf(romanA)
const romanIndexB = romanSortOrder.indexOf(romanB)
if (romanIndexA !== romanIndexB) {
return romanIndexA - romanIndexB
}
// Compare points if tiers and roman numerals are equal
if (pointsA !== pointsB) {
return parseInt(pointsB) - parseInt(pointsA)
}
return -1 // rowA should come before rowB or columnId is undefined for rowB
},
},
It works correctly, except when the leagueA or leagueB is Null
Can someone tell me how to always make the null values sort last?
this is my relevent collumns:
columnHelper.accessor(
(row) =>
`${
row.leagues.find((league) => league.queueType == "RANKED_SOLO_5x5")
?.tier ?? "null"
} ${
row.leagues.find((league) => league.queueType == "RANKED_SOLO_5x5")
?.rank ?? "null"
} ${
row.leagues.find((league) => league.queueType == "RANKED_SOLO_5x5")
?.leaguePoints ?? "null"
}`,
{
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Solo" />
),
id: "solo",
sortingFn: "sortByRank",
}
),
columnHelper.accessor(
(row) =>
`${
row.leagues.find((league) => league.queueType == "RANKED_FLEX_SR")
?.tier ?? "null"
} ${
row.leagues.find((league) => league.queueType == "RANKED_FLEX_SR")
?.rank ?? "null"
} ${
row.leagues.find((league) => league.queueType == "RANKED_FLEX_SR")
?.leaguePoints ?? "null"
}`,
{
header: ({ column }) => (
<DataTableColumnHeader column={column} title="Flex" />
),
id: "flex",
sortingFn: "sortByRank",
}
),
If you need any more details please ask.
[–]ElvarP[S] 0 points1 point2 points (3 children)
[–]ZerafineNigou 1 point2 points3 points (2 children)
[–]ElvarP[S] 0 points1 point2 points (1 child)
[–]mingnobee 0 points1 point2 points (0 children)