-3

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", "", ""].

nurnachman
  • 4,468
  • 2
  • 37
  • 40
Anuja
  • 124
  • 2
  • 6
  • 2
    Have you tried your code? Running your code would result to `["Anuja", "Ranbir", "undefined", undefined]`. – bertdida Aug 12 '20 at 09:26
  • Is `"undefined"` string also considered as `undefined`? Do you want to set the `undefined` to empty string Or you just want to sort the array? – adiga Aug 12 '20 at 09:26
  • `"undefined"` is a normal string and is not equal to `undefined` so it will be normal sorted. – Sascha Aug 12 '20 at 09:28
  • 2
    Note that assigning "" to `a` or `b` in the callback does not change values in your array. You should first test your code and then come with your question. – trincot Aug 12 '20 at 09:28
  • 1
    Don't forget that uppercases sorted before lowercases, so you will return i.e. `['Alf','Peter','Tom','blue','undefined','young',undefined]` – Sascha Aug 12 '20 at 09:33
  • I was typing an answer but it got locked :( I would propose to map your array before sorting it then you can easily sort it from a to b with undefined (now "") as the last element. var content = ["Anuja", undefined, "Ranbir", "undefined"]; // first map your array content = content.map(c => { if(c === "undefined" || c === undefined) return c = ""; return c }) // then sort it var sorted = content.sort(function(a, b) { return a.localeCompare(b); }); console.log(sorted) see https://jsfiddle.net/45v80agd/ – 今際のアリス Aug 12 '20 at 09:38
  • `var newContent = content.sort().map(val=>val==='undefined'||typeof val==='undefined'?val='':val=val)` try this , be careful using sort() – KcH Aug 12 '20 at 09:40
  • Dupe: https://stackoverflow.com/questions/29829205/sort-an-array-so-that-null-values-always-come-last – mplungjan Aug 12 '20 at 09:40
  • Guys i don't care if it is an empty string or undefined. I just want it to come in the end. The whole point has been moved out of context. – Anuja Aug 12 '20 at 09:52
  • you could just use sort() for that (case sensitive matters) – KcH Aug 12 '20 at 10:02
  • @ヨハンソン - the map will ignore undefined! – mplungjan Aug 14 '20 at 05:47

1 Answers1

1

It was not trivial but I found a way

let content = ["Anuja", null, "Ranbir", ,null]; // note the undefined entry at index 3

// one of the few ways to detect undefined entries
// .map IGNORES undefined entries
for (let i=0;i<content.length;i++) {
  content[i] = content[i] || ""; // you may want to test for 0 here
}

const asc = (a, b) => {
  if (a === "") return 1; // can be DRY'd using *dir where dir is -1 or 1
  if (b === "") return -1;
  return a.localeCompare(b);
};
const dsc = (a, b) => {
  if (a === "") return -1;
  if (b === "") return 1;
  return b.localeCompare(a);
}


content.sort(asc);
console.log(content)
content.sort(dsc);
console.log(content)
mplungjan
  • 169,008
  • 28
  • 173
  • 236