cherry-studio/scripts/sort.ts
icarus 534459dd13 refactor(sort): replace lexicalSort with naturalSort for better string comparison
Use localeCompare with numeric sensitivity for more natural string sorting behavior
2025-10-23 21:50:21 +08:00

52 lines
1.4 KiB
TypeScript

// https://github.com/Gudahtt/prettier-plugin-sort-json/blob/main/src/index.ts
/**
* Lexical sort function for strings, meant to be used as the sort
* function for `Array.prototype.sort`.
*
* @param a - First element to compare.
* @param b - Second element to compare.
* @returns A number indicating which element should come first.
*/
function lexicalSort(a: string, b: string): number {
if (a > b) {
return 1
}
if (a < b) {
return -1
}
return 0
}
/**
* Natural sort function for strings, meant to be used as the sort
* function for `Array.prototype.sort`.
*
* @param a - First element to compare.
* @param b - Second element to compare.
* @returns A number indicating which element should come first.
*/
function naturalSort(a: string, b: string): number {
return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' })
}
/**
* Sort object keys in dictionary order (supports nested objects)
* @param obj The object to be sorted
* @returns A new object with sorted keys
*/
export function sortedObjectByKeys(obj: object): object {
const sortedKeys = Object.keys(obj).sort(naturalSort)
const sortedObj = {}
for (const key of sortedKeys) {
let value = obj[key]
// If the value is an object, sort it recursively
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
value = sortedObjectByKeys(value)
}
sortedObj[key] = value
}
return sortedObj
}