I need to sort a list of data by alphabetical order but in A-a-Z-z, were the name Antony
comes before the name antony
and Zelda
comes before zelda
so the list looks like this:
- Abigail
- Antony
- abigail
- antony
- Zelda
- zelda
The basic
list.sort(function (a, b) {
if (a.name > b.name) return -1;
if (a.name < b.name) return 1;
return 0;
});
is producing a list like this:
preferable language: Javascript
There is a built-in for that:
let list = ["abigail", "Antony", "Abigail", "antony", "Zelda", "zelda"];
list.sort((a, b) =>
a.localeCompare(b, "en", { caseFirst: "upper" })
);
console.log(list);
EDIT: maybe you want this?
let list = ["abigail", "Antony", "Abigail", "antony", "Zelda", "zelda"];
const compareUpperFirst = (a, b) => {
if (a === "" && bb === "") return 0;
if (a === "") return -1;
if (b === "") return 1;
let aa = a.charAt(0);
let aal = aa.toLowerCase();
let bb = b.charAt(0);
let bbl = bb.toLowerCase();
if (aal < bbl) return -1;
if (aal > bbl) return 1;
if (aa < bb) return -1;
if (aa > bb) return 1;
return compareUpperFirst(a.substr(1), b.substr(1));
};
list.sort(compareUpperFirst);
console.log(list);