Completed
Last Updated: 04 Jan 2024 07:42 by ADMIN
Jason
Created on: 27 Sep 2017 15:46
Category: Grid
Type: Feature Request
25
Be able to overrride compare method in kendo-data-query sort-array.operator
We need to be able to overrride "compare" method in kendo-data-query sort-array.operator.  We are using your processData for client side sorting but need to change the "compare" method since we have decimal values already formatted in our objects.  Since they are formatted they are technically strings and sorted as such.  Currently we had to rip out your processData and call our custom version with a compare method like so:

const compare = (a, b) => {
    if (isBlank(a)) {
        return a === b ? 0 : -1;
    }

    if (isBlank(b)) {
        return 1;
    }

    let a1 = a;
    if (isNumber(a)) {
        a1 = toNumber(a);
    } else if (isDate(a)) {
        a1 = ensureDate(a);
    }

    let b1 = b;
    if (isNumber(b)) {
        b1 = toNumber(b);
    } else if (isDate(b)) {
        b1 = ensureDate(b);
    }

    if (a1.localeCompare) {
        return a1.localeCompare(b1);
    }

    return a1 > b1 ? 1 : (a1 < b1 ? -1 : 0);
};
16 comments
ADMIN
Yanmario
Posted on: 04 Jan 2024 07:42

Hi Clin,

I consulted our developers on this matter and the compare function determines the order of the elements without a specific direction given by a user. For reference the Array compare function:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#comparefn

If utilized in a Grid or another component dependent on sort descriptors, the descriptors revealed in the events will possess a direction. Depending on this direction, you can pass a different callback (or dynamically replace the logic within the callback), for instance:

https://stackblitz.com/edit/angular-jxdrba-6rhgyx?file=src%2Fapp%2Fapp.component.ts

The example demonstrates the sorting of the Product Name based on text length.

Regards,
Yanmario
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Clint Singer
Posted on: 28 Dec 2023 15:18

While the solution from Yanmario is a possible workaround.  I agree with Rakshith that the native signature should have had the descending property included from the beginning.

The workaround is ugly in that you have to physically track the sort direction and pass the sort for the specific sort descriptor.   If you have a number of them and you decide to remove any of them then you also have to remember to update the the indexes.  

ADMIN
Yanmario
Posted on: 28 Dec 2023 08:16

Hi Rakshith,

The SortDescriptor has a dir property that can be set to a specific direction by the developer. If you would like to pass the direction to the custom compare function, then it can be achieved the following way:

  public sort: SortDescriptor[] = [
    {
      field: 'ProductName',
      dir: 'desc',
      compare: (a, b) => this.customCompare(a, b, this.sort[0].dir),
    },
  ];

https://stackblitz.com/edit/angular-mvh42d?file=src%2Fapp%2Fapp.component.ts

Do let me know if you have something else in mind and I will also consult the matter with our developers at that point for further clearance. 

Regards,
Yanmario
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Rakshith K A
Posted on: 21 Dec 2023 15:21

Thanks for considering this request. However seeing slight deviation from JQuery Api where direction of sort is missing in compare interface.

https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/configuration/columns.sortable#columnssortablecompare

 function compare(a, b, descending) {

One notable exception is that we also supply a third parameter that indicates the sort direction (true for descending)

However in Angular we are not seeing the order of sorting. compare? (a: any, b: any) => number

Could you pass order of sorting as well like JQuery. Without order there is no meaning of custom comparator. 

 

ADMIN
Martin Bechev
Posted on: 20 Dec 2023 07:34

Hi all,

I am glad to announce that the compare property is now available and part of the SortDescriptor:

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

Please try it out and let us know in case of any feedback.

Regards,
Martin
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Rakshith K A
Posted on: 15 Dec 2023 06:33

Hello Team, This feature is very useful for column sorting. This request is open from last 5year.

We dont want to write all sort algorithm from scratch and update the UX. 

Library provide this feature for Jquery https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/configuration/columns.sortable#columnssortablecompare

After analyzing the sort source code, we see composeSortDescriptors is encapsulating the compare callback. We need a hook to override these function.

 

 

 

Clint Singer
Posted on: 11 Jan 2023 23:51

Actually having reviewed this again, it dawned on me that my proposal isn't very good. 

Rather than 

comparers: [{ field: 'productName', comparer: compare}],

The comparer should just be a property of the original sort descriptor

sort: [{ field: 'productName', dir: 'desc', comparer: compare }]

There should be a way to know if you are sorting ascending or descending from the comparer.   I have a scenario where I want certain rows to always be at the end of the grid regardless of the direction the fields are sorted, but I wouldn't know what to do with the compare result unless I also knew the direction.

The advantage to putting it on the SortDescriptor directly is two-fold.  First it makes sense, and second the same pattern could be used for filtering and grouping since they would also benefit from having the ability to customize the logic for those on a per column basis.

For example, I would like to create custom group logic where a time column can be grouped by hours or by days (I don't believe you can do that now).

If I could put in more votes for this I would certainly try to move it to the top.  As I stated before, the feature is almost there.  The composeSortDescriptors() just ends up injecting simple compare or compareDesc functions.  Just need a way to swap those out with our own!

 

ADMIN
Martin Bechev
Posted on: 02 Nov 2022 08:51

Hi all,

Thank you for the provided feedback, and possible suggestions on how the requested feature could be implemented.

We will take into consideration the proposals during the development process.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Clint Singer
Posted on: 26 Oct 2022 21:23
I was just stepping through the implementation of the process() call in kendo angular and 99% of what one needs to make this happen is already in the library.  It just needs some way to inject the custom comparer functions.  I think my proposal is easy enough and still fits the spirit of the feature.
Clint Singer
Posted on: 26 Oct 2022 21:01

I'm still interested in this feature... it is a big hole in the grid sorting capabilities. 

I would think that adding a "comparers" field in the vein of how the "sort" property looks would be a good way to go about it. 

Sort looks like:

sort: [{ field: 'productName', dir: 'desc' }],

Comparers could map something like (assuming the method defined in the original post)

comparers: [{ field: 'productName', comparer: compare}],

When the sort does its thing, it looks in the comparers list and if it finds a field match then use that function instead of its built-in one.

Boris
Posted on: 07 Oct 2022 10:26

Will this feature ever be acomplished? It would be very hapeful to have the ability to sort columns, based on customized logic and not just based on column type. As far as I'm concerned, this is available in the jQuery implementation but fore some reason it's missing from Angular UI implementation. I believe it shouldn't be that hard to just extend the SortDescriptor to accept additional 'compare' callback function. It's very frustrating, because I can't find any workaround at the moment, since sortChange() event handler can't be used in our case, because we combine multiple grid functionalities - sorting, grouping, filtering.

Thanks

Danail
Posted on: 05 Oct 2022 11:55
Lets come back again after 5 years
ADMIN
Silviya
Posted on: 06 Jul 2022 07:05

Hello, Cyrill,

Sure, no problem.

Have a great day at work!

Regards,
Silviya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Cyril
Posted on: 29 Jun 2022 11:52
Great, thanks for the heads up Silviya!
ADMIN
Silviya
Posted on: 29 Jun 2022 11:12

Hi Cyril,

Indeed, the feature request is recognized as valid, and based on votes seems to be popular.

I will raise it for discussion within the development team.

In general, we are monitoring the demand for each logged feature request, and based on multiple factors such as votes, business value, resources, and others we decide which feature to prioritize. I can't share an exact timeline on when the functionality will be available. Usually, tracking our roadmap can give an idea of what we are currently working on.

Regards,
Silviya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Cyril
Posted on: 22 Jun 2022 14:27

Hello,

This feature request has been open for almost 5 years. It would be very useful for our use case, otherwise we would have to develop complex and hard-to-maintain code.

We already shared additional details (see internal ticket #1516595), but it would be nice to have a feedback from Kendo on this feature (it's the fourth most requested feature :) )

 

Thanks