// The Lucia project. // Copyright 2023-2026 DSP, inc. All rights reserved. // Authors: // chiayin.kuo@dsp.im (chiayin), 2023/1/31 // imacat.yang@dsp.im (imacat), 2023/9/23 // cindy.chang@dsp.im (Cindy Chang), 2024/5/30 /** @module sortNumEngZhtw Sorting for mixed numeric/text data. */ /** * Sorts an array of mixed numeric and text values in place. * * Numbers come first (ascending), followed by text sorted by * Traditional Chinese locale (zh-Hant-TW). * * @param {Array} data - The array to sort. * @returns {Array} The sorted array (same reference). */ export function sortNumEngZhtw(data) { return data.sort((a, b) => { const isANumber = !isNaN(parseFloat(a)) && isFinite(a); const isBNumber = !isNaN(parseFloat(b)) && isFinite(b); if (isANumber && isBNumber) return parseFloat(a) - parseFloat(b); if (isANumber) return -1; if (isBNumber) return 1; return a.localeCompare(b, "zh-Hant-TW", { sensitivity: "accent" }); }); } /** * Comparator function for sorting mixed numeric and text values. * * Suitable for use as an Array.sort() callback. Numbers sort before * text, and text is sorted by Traditional Chinese locale. * * @param {string|number} a - First value to compare. * @param {string|number} b - Second value to compare. * @returns {number} Negative if a < b, positive if a > b, zero if equal. */ export function sortNumEngZhtwForFilter(a, b) { const isANumber = !isNaN(parseFloat(a)) && isFinite(a); const isBNumber = !isNaN(parseFloat(b)) && isFinite(b); if (isANumber && isBNumber) return parseFloat(a) - parseFloat(b); if (isANumber) return -1; if (isBNumber) return 1; return a.localeCompare(b, "zh-Hant-TW", { sensitivity: "accent" }); }