Why Firefox 105.0 (64 bits) and MS Edge Version 105.0.1343.42 (Version officielle) (64 bits) sorts differently?

Viewed 22

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>
1 Answers

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.

Related