Skip to content

Instantly share code, notes, and snippets.

@nexpr
Created November 26, 2023 12:44
Show Gist options
  • Save nexpr/fdb7c976d9b90ebb894a5df839725ada to your computer and use it in GitHub Desktop.
Save nexpr/fdb7c976d9b90ebb894a5df839725ada to your computer and use it in GitHub Desktop.
sort domain
export const sortDomain = (domains) => {
return domains.map(x => x.split(".")).sort((a, b) => {
const max = Math.max(a.length, b.length)
const recur = (i = 0) => {
if (i >= max) return 0
return (a.at(-i - 1) ?? "").localeCompare(b.at(-i - 1) ?? "") || recur(i + 1)
}
return recur()
}).map(x => x.join("."))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment