53 lines
1.9 KiB
JavaScript
53 lines
1.9 KiB
JavaScript
// 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<string|number>} data - The array to sort.
|
|
* @returns {Array<string|number>} The sorted array (same reference).
|
|
*/
|
|
export function sortNumEngZhtw(data) {
|
|
return data.sort((a, b) => {
|
|
const isANumber = !Number.isNaN(Number.parseFloat(a)) && Number.isFinite(Number(a));
|
|
const isBNumber = !Number.isNaN(Number.parseFloat(b)) && Number.isFinite(Number(b));
|
|
|
|
if (isANumber && isBNumber) return Number.parseFloat(a) - Number.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 = !Number.isNaN(Number.parseFloat(a)) && Number.isFinite(Number(a));
|
|
const isBNumber = !Number.isNaN(Number.parseFloat(b)) && Number.isFinite(Number(b));
|
|
|
|
if (isANumber && isBNumber) return Number.parseFloat(a) - Number.parseFloat(b);
|
|
|
|
if (isANumber) return -1;
|
|
if (isBNumber) return 1;
|
|
|
|
return a.localeCompare(b, "zh-Hant-TW", { sensitivity: "accent" });
|
|
}
|