From 044d5307bc7f9f0d130e88b93272ce11545dad5b Mon Sep 17 00:00:00 2001 From: Kentai Radiquum Date: Sun, 18 Aug 2024 11:51:36 +0500 Subject: [PATCH] fix: sort previous versions by descending order --- app/api/version/route.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/app/api/version/route.ts b/app/api/version/route.ts index b2bf6d7..12dcc7a 100644 --- a/app/api/version/route.ts +++ b/app/api/version/route.ts @@ -13,5 +13,32 @@ export async function GET() { } }); - return NextResponse.json({ version: current, previous: previous }); + function compare(a: string, b: string) { + const aElement = a.split("."); + const bElement = b.split("."); + + const aLength = aElement.length; + const bLength = bElement.length; + + let order = 0; + + for (let i = 0; i < Math.max(aLength, bLength); i++) { + const aNum = Number(aElement[i] || 0); + const bNum = Number(bElement[i] || 0); + if (aNum !== bNum) { + if (a > b) { + order += -1; + } + if (a < b) { + order += 1; + } + } + } + return order; + } + + return NextResponse.json({ + version: current, + previous: previous.sort(compare), + }); }