Skip to main content
added 1 character in body
Source Link
fusion3k
  • 11.7k
  • 4
  • 27
  • 47

If you want to sort by ascending it should be the comparator function should return a - b not a.length - b.length because a.length - b.length the result will always be zero for all the alphabets of length 1. The result will be different for ['ab', 'a'] will produce ['a', 'ab'] because the length of first element in array is lesser than second element.

function longest(arr) {
  return arr.sort( (a,b) => {
    return a - b;
  });
}

}

If you want to sort by ascending it should be the comparator function should return a - b not a.length - b.length because a.length - b.length the result will always be zero for all the alphabets of length 1. The result will be different for ['ab', 'a'] will produce ['a', 'ab'] because the length of first element in array is lesser than second element.

function longest(arr) {
  return arr.sort( (a,b) => {
    return a - b;
  });

}

If you want to sort by ascending it should be the comparator function should return a - b not a.length - b.length because a.length - b.length the result will always be zero for all the alphabets of length 1. The result will be different for ['ab', 'a'] will produce ['a', 'ab'] because the length of first element in array is lesser than second element.

function longest(arr) {
  return arr.sort( (a,b) => {
    return a - b;
  });
}
added 198 characters in body
Source Link

If you want to sort by ascending it should be as below, when we usethe comparator function should return a - b not a.length - b.length because a.length - b.length the result will always be zero andfor all the alphabets of length 1. The result will not be different for ['ab', 'a'] will produce ['a', 'ab'] because the length of first element in orderarray is lesser than second element.

function longest(arr) {
  return arr.sort( (a,b) => {
    return a - b;
  });

}

If you want to sort by ascending it should be as below, when we use a.length - b.length the result will always be zero and the result will not be in order.

function longest(arr) {
  return arr.sort( (a,b) => {
    return a - b;
  });

}

If you want to sort by ascending it should be the comparator function should return a - b not a.length - b.length because a.length - b.length the result will always be zero for all the alphabets of length 1. The result will be different for ['ab', 'a'] will produce ['a', 'ab'] because the length of first element in array is lesser than second element.

function longest(arr) {
  return arr.sort( (a,b) => {
    return a - b;
  });

}

Source Link

If you want to sort by ascending it should be as below, when we use a.length - b.length the result will always be zero and the result will not be in order.

function longest(arr) {
  return arr.sort( (a,b) => {
    return a - b;
  });

}