JavaScript - Grid - Sorting result not as expected

Hi Team,

I have enabled the default Group settings with the descending order direction, but the expected was not achievable.

Please refer the link for sample: Xebn6q (forked) - StackBlitz


Expected Output:
Ascending: S5, S50, S200, S300, S600, S650

Descending: S650, S600, S300, S200, S50, S5


Regards,

Sugu


1 Reply

DM Dineshnarasimman Muthu Syncfusion Team February 1, 2024 03:45 AM UTC

Hi Sugu Maran,


Greetings from Syncfusion Support.


We have reviewed your requirement about sorting the records numeric and non-numericYou can achieve similar sorting functionality by splitting the numeric and non-numeric parts of the reference and comparer using regular expressions and comparing them appropriately. We have prepared a code snippet that resembles Natural Sort using the sortComparer function, which you can find below:


function sortComparer(referencecomparer) {

  var referenceParts = [],

    comparerParts = [];

  reference.replace(/(\d+)|(\D+)/gfunction (_$1$2) {

    referenceParts.push([$1 || Infinity$2 || '']);

  });

  comparer.replace(/(\d+)|(\D+)/gfunction (_$1$2) {

    comparerParts.push([$1 || Infinity$2 || '']);

  });

  while (referenceParts.length && comparerParts.length) {

    var referencePart = referenceParts.shift();

    var comparerPart = comparerParts.shift();

    var comparisonResult =

      referencePart[0] - comparerPart[0] ||

      referencePart[1].localeCompare(comparerPart[1]);

    if (comparisonResultreturn comparisonResult;

  }

  return referenceParts.length - comparerParts.length;

}

 



Using the above logic, you can sort the values in the columns in Natural Order. We have modified the sample you provided with the code snippet above. You can find the modified sample attached below.



Sample: https://stackblitz.com/edit/cjg4fs-qnug1z?file=index.ts


Documentation: https://ej2.syncfusion.com/javascript/documentation/grid/sorting#custom-sort-comparer


Please let us know if you need any further assistance.


Regards,

Dineshnarasimman M


Loader.
Up arrow icon