From b3f8e8ca8e136345882c3468bc2be9af31a12023 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Sun, 29 Sep 2024 11:10:25 +0800 Subject: [PATCH] fix: display index sort --- .../ClientComponents/ServerListClient.tsx | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx index c111d03..ab96e56 100644 --- a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx +++ b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx @@ -14,19 +14,33 @@ export default function ServerListClient() {

{error.message}

- Please check your environment variables and review the server console logs for more details. + Please check your environment variables and review the server console + logs for more details.

); if (!data) return null; - const sortedServers = data.result.sort((a, b) => { - if (a.display_index && b.display_index) { - return b.display_index - a.display_index; - } - if (a.display_index) return -1; - if (b.display_index) return 1; - return a.id - b.id; - }); + + const { result } = data; + + const positiveDisplayIndex = result + .filter((server) => server.display_index > 0) + .sort((a, b) => b.display_index - a.display_index); + + const noDisplayIndex = result + .filter((server) => !server.display_index) + .sort((a, b) => a.id - b.id); + + const negativeDisplayIndex = result + .filter((server) => server.display_index < 0) + .sort((a, b) => b.display_index - a.display_index); + + const sortedServers = [ + ...positiveDisplayIndex, + ...noDisplayIndex, + ...negativeDisplayIndex, + ]; + return (
{sortedServers.map((serverInfo) => (