Megabit / Blazorise

Blazorise is a component library built on top of Blazor with support for CSS frameworks like Bootstrap, Tailwind, Bulma, AntDesign, and Material.
https://blazorise.com/
Other
3.31k stars 534 forks source link

Custom Sorting for custom compare method #5839

Closed sonali-vaghela closed 1 week ago

sonali-vaghela commented 2 weeks ago

In my project there is a requirement where data is a combination of alphanumeric. I want to define custom sort function that if it contains numbers at the end then it will need to sort like numeric sorting not string sorting and if there are no numbers then it will behave like string sorting.

Just like WPF grid Compare(object x, object y) method.

Is there any way possible for this?

stsrki commented 2 weeks ago

Is this for DataGrid? There is a SortField on the DataGridColumn that you can use for custom sorting.

sonali-vaghela commented 2 weeks ago

Yes, It is for Datagrid. But there is no specific sort field. Let's say there is "Label" field. But I want to sort it using custom sort method because my label field contains both string and numbers. If it contains numbers then it will first sort as numbers and if not that it will sort as string. So there is no separate sort field. Fox Example : Data is : [A-1, A-2, A-10, A-100, A-11, XYZ, ABC] I want sorting like [A-1, A-2, A-10, A-11, A-100, ABC, XYZ] For this how can i define sort field?

stsrki commented 2 weeks ago

You only need to add an additional field with the sort logic in it.

class MyDto
{
  string Label { get; set; }
  string MyLabelSort 
  {
    if ( something )
      return "A";

    return "B";
  }
}
sonali-vaghela commented 2 weeks ago

As you can see from my example there is nothing like simple if condition or data modification. It's like mix of string and alphanumeric data. And I need to set it based on comparison of previous and next data in the list. Check if the list has other data. So it's not like just returning one value. it's like comparing through the data of list for sorting

stsrki commented 2 weeks ago

It seems like you only need to pad a value to make it sortable

public string SortKey
{
    get
    {
        var parts = Label.Split( '-' );

        if ( parts.Length == 2 && int.TryParse( parts[1], out int number ) )
        {
            return $"{parts[0]}-{number:D5}";
        }

        return Label;
    }
}
sonali-vaghela commented 1 week ago

There is no fix syntax. It's just an example. There are many combinations. It combines For that reason I need to write some custom compare method logic

Another Example :

Data : [planta 01, planta 11, planta 10, planta5f, planta5c, planta111] Output expected : [planta111, planta 01, planta 10, planta 11, planta5c, planta5f]

So it want be possible to format numbers.

stsrki commented 1 week ago

That is something that we don't support at the moment. But we will try to make it, if possible, by the end of this week. Just for 1.7 release.

stsrki commented 1 week ago

Possible use case could e

<DataGridColumn SortFieldFunc="@((item)=>item.Something)" >

tesar-tech commented 1 week ago

@sonali-vaghela To better understand the issue. Can you plugin your sorting code into this sharplab code?

Either it implements its own Func<string, object> OrderBy function.

or

It implements a comparer..

Both is doable. I guess you want the comparer, but I want to make sure it will suit your needs.

Currently the implementation compares based on string length.. and of course there will be TItem instead of string, but for now, bare with me..

sonali-vaghela commented 1 week ago

Hi @tesar-tech,

I have implemented my comparer method code in this https://sharplab.io/#gist:7d50cd1f2f21df6a475d44a3ed0a7c7c You can check this.