0

Why this sort task does not perform in MS Edge, but it acts very well in the firefox ?

<script type="text/javascript">"use strict";
  const arr = ["stella","gamma","area","benzyl","xtra"];
  console.log(arr.sort((x,y)=>x>y));
  console.log(arr.sort((x,y)=>x-y));

  // MS Edge Version 105.0.1343.42 (Version officielle) (64 bits) :
  // (5) ['stella', 'gamma', 'area', 'benzyl', 'xtra']
  // (5) ['stella', 'gamma', 'area', 'benzyl', 'xtra']

  // Firefox 105.0 (64 bits) :
  // Array(5) [ "area", "benzyl", "gamma", "stella", "xtra" ]
  // Array(5) [ "area", "benzyl", "gamma", "stella", "xtra" ]
</script>
  • Hi @Doct Jean-Dadet Luyalu. May I know if you have got any chance to check my answer? I am glad to help if you have any other questions. – Kendrick Li Sep 27 '22 at 08:56

1 Answers1

0

AFAIK, chromium browsers use a different sort function (QuickSort) from Firefox (MergeSort), which leads to discrepancies in results.

Normally, in order to sort strings, we just use array.sort(), and it works fine on both Edge and Firefox.

As for array.sort((x,y)=>x>y), normally we don't write such code, but we can expect that Firefox has modified its source code to make it work. For more information, you can take a look at this thread.

As for array.sort((x,y)=>x-y), it does not work in both browsers for sorting strings. In this case, it displays the well sorted array just because the array.sort((x,y)=>x>y) has already rearranged the original array. It is used for number sorting.

Kendrick Li
  • 1,355
  • 1
  • 2
  • 10
  • Does stuff at ms-edge know about their weakness and discrepancy to normalize behaviour of all the browsers ? – Doct Jean-Dadet Luyalu Oct 16 '22 at 12:49
  • In fact, it's a feature shared by all chromium browsers, including Edge, Chrome and Opera. It is more of a difference than weakness. They know the differences for sure. – Kendrick Li Oct 17 '22 at 02:14