diff --git a/src/components/modules/table/memberHeaderColumns.tsx b/src/components/modules/table/memberHeaderColumns.tsx index 5ce3a6b5..bcbd056b 100644 --- a/src/components/modules/table/memberHeaderColumns.tsx +++ b/src/components/modules/table/memberHeaderColumns.tsx @@ -5,7 +5,7 @@ import { useModalStore } from "~/utils/store"; import toast from "react-hot-toast"; import { useTranslations } from "next-intl"; import TimeAgo from "react-timeago"; -import { type ColumnDef, createColumnHelper } from "@tanstack/react-table"; +import { type ColumnDef, createColumnHelper, Row } from "@tanstack/react-table"; import { type NetworkMemberNotation, type MemberEntity } from "~/types/local/member"; enum ConnectionStatus { @@ -21,6 +21,41 @@ interface IProp { central: boolean; } +const sortingMemberHex = ( + rowA: Row, + rowB: Row, + columnId: string, +): number => { + const a = rowA.original[columnId] as string; + const b = rowB.original[columnId] as string; + const numA = BigInt(`0x${a}`); + const numB = BigInt(`0x${b}`); + + if (numA > numB) return 1; + if (numA < numB) return -1; + return 0; +}; + +const sortIP = (ip: string) => { + return ip + .split(".") + .map(Number) + .reduce((acc, val) => acc * 256 + val); +}; + +const sortingIpaddress = ( + rowA: Row, + rowB: Row, + columnId: string, +): number => { + const a = rowA.original[columnId] as string[]; + const b = rowB.original[columnId] as string[]; + const numA = a.length ? sortIP(a[0]) : 0; + const numB = b.length ? sortIP(b[0]) : 0; + + return numA - numB; +}; + export const MemberHeaderColumns = ({ nwid, central = false }: IProp) => { const t = useTranslations(); const { callModal } = useModalStore((state) => state); @@ -92,6 +127,7 @@ export const MemberHeaderColumns = ({ nwid, central = false }: IProp) => { columnHelper.accessor("name", { header: () => {t("networkById.networkMembersTable.column.name")}, id: "name", + sortingFn: sortingMemberHex, }), columnHelper.accessor("id", { header: () => {t("networkById.networkMembersTable.column.id")}, @@ -103,6 +139,7 @@ export const MemberHeaderColumns = ({ nwid, central = false }: IProp) => { {t("networkById.networkMembersTable.column.ipAssignments.header")} ), id: "ipAssignments", + sortingFn: sortingIpaddress, }), columnHelper.accessor("creationTime", { header: () => {t("networkById.networkMembersTable.column.created")},