I have an array of strings that I want to sort. The array could contain some values that are undefined. In those cases, for Ascending order, the values should come in the end, whereas in Descending order, the values should come in the beginning.
I tried
var content = ["Anuja", undefined, "Ranbir", "undefined"];
content.sort(function(a, b) {
if (a == undefined) {
a = ""
}
if (b == undefined) {
b = ""
}
return a.localeCompare(b);
});
console.log(content)
With this code, I get ["","", "Anuja", "Ranbir"] but my requirement is ["Anuja", "Ranbir", "", ""].