This is a migrated thread and some comments may be shown as answers.

Sorting a String column gives wrong results when numbers are included

1 Answer 1515 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bilal
Top achievements
Rank 2
Bilal asked on 20 May 2015, 06:27 AM

Hi,

One of the columns is an ID of a record. Id has a certain format something like: XXXX-XXX-1 etc.

 When sorting on this column values show as:

XXXX-XXX-1

XXXX-XXX-10

XXXX-XXX-2

XXXX-XXX-3

...

 

Is there a way to properly sort such that -10 appears after?

 

Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Plamen Lazarov
Telerik team
answered on 21 May 2015, 03:56 PM

Hello Bilal,

The reason for this behavior is because by default JavaScript sort() method sorts elements alphabetically. I would offer to take a look at  Array.prototype.sort() article that explains the same.

I would suggest to take a look at columns.sortable.compare resource which gives the opportunity to specify custom sorting function. In order to sort numerically it would be necessary to implement a custom sorting logic. 

Let us know if that helps.

Regards,
Plamen Lazarov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Sandip
Top achievements
Rank 1
commented on 04 Jan 2024, 06:59 AM | edited

Hi,

Is there any solution in kendo ui angular ?

 

Thanks,

Sandip

Georgi
Telerik team
commented on 05 Jan 2024, 03:25 PM

Hi Sandip,

Thank you very much for the details provided.

One possible approach to modify the default sorting of the Kendo UI for Angular Grid component would be for the developer to utilize the built-in compare property of the SortDescriptor object and thus determine the sorting logic:

https://www.telerik.com/kendo-angular-ui/components/data-query/api/SortDescriptor/#toc-compare-

To better illustrate this suggestion, I am sending you a StackBlitz demo that demonstrates a possible approach for providing a custom compare function for the Grid's sorting:

https://stackblitz.com/edit/angular-mvh42d-yr3bar

I hope this helps. Please, let me know if I am missing out on something.

Regards,
Georgi
Progress Telerik
Tags
Grid
Asked by
Bilal
Top achievements
Rank 2
Answers by
Plamen Lazarov
Telerik team
Share this question
or