How do I do a case-insensitive sorting using the SFDataGrid?

I can find no place to define a callback or pass in a comparer object to do the sorting in a case-insensitive way. Thanks!

P.S. This question may already have been asked and answered on this forum but there's no way to know, since there seems to be no way to filter the search results to just the Flutter forum. Sorry if I'm duplicating work here.

13 Replies 1 reply marked as answer

NK Neelakandan Kannan Syncfusion Team October 5, 2020 07:21 AM UTC

Hi Chris,

Thanks for contacting Syncfusion support.

SfDataGrid supports to sort the data in custom way by handling the handleSort method in DataGridSource. handleSort method will be called whenenever the column is sorted. We have already documented to sort the data by its length. Please find the UG link in below,
https://help.syncfusion.com/flutter/datagrid/sorting#custom-sorting

Please let me know if you have any concerns.


CS Chris Sells October 5, 2020 04:04 PM UTC

Thanks, Neelakandan. That's a useful start. Do you have a sample for sorting multiple columns? Do I sort them one at a time? Front-to-back? Back-to-front? Something else? Thanks!


NK Neelakandan Kannan Syncfusion Team October 6, 2020 02:18 PM UTC

Hi Chris,


Thanks for your update.


If you want to perform the sorting for multi columns, you can set the allowMultiColumnSorting property as true. To apply sorting for multiple columns in web, you can click the column header by pressing the CTRL key.

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text(widget.title),

      ),

      body: SfDataGrid(

        source: _sortingDataGridSource,

        allowSorting: true,

        allowMultiColumnSorting: true,

        columnWidthMode: ColumnWidthMode.auto,

        columns: getColumns(),

      ),

    );

  }


Currently, if you want to perform the custom sorting for multiple columns, you should write the logic to sort the multiple columns like we did in our SfDataGrid internally. In the below sample, I have provided the simple example to perform the case insensitive sorting for text columns (name and city). It also supports to apply the sorting for other columns too.

https://www.syncfusion.com/downloads/support/forum/158389/ze/multi_column_sorting_datagrid-993288601.zip


Meanwhile, we have a plan to expose the compare method (The method that we use to compare the two objects) in DataGridSource class. So that, you can return the case insensitive value for comparison in single place rather than writing entire logic for sorting in handleSort method. We will include this in our weekly nuget release which is scheduled on October 20th 2020.


Please let us know if you have any concerns.


Regards,

Neelakandan



CS Chris Sells October 6, 2020 05:32 PM UTC

Thanks very much for the sample and for the update on the upcoming release that will make this easier!

When the update comes, how should I write my code?


NK Neelakandan Kannan Syncfusion Team October 7, 2020 01:33 PM UTC

Hi Chris,

Thanks for the update.

We will analyze the way to provide the API for this requirement  and let you know how you can write the code on 9th October 2020.

Regards,
Neelakandan


NK Neelakandan Kannan Syncfusion Team October 9, 2020 04:22 PM UTC

Hi Chris,

Thanks for your patience.

We will expose the compare method in DataGridSource class. You can simply override that method and return the value based on your requirement. Please find the example code,

final EmployeeDataSource _employeeDataSource = EmployeeDataSource();

 

class EmployeeDataSource extends DataGridSource<Employee> {

  @override

  List<Employee> get dataSource => _employees

 

  @override

  getValue(Employee employee, String columnName) {

    switch (columnName) {

      case 'id':

        return employee.id;

        break;

      case 'name':

        return employee.name;

        break;

      case 'salary':

        return employee.salary;

        break;

      case 'designation':

        return employee.designation;

        break;

      default:

        return ' ';

        break;

    }

  }

  @override

  int compare(Employee value1, Employee value2, SortColumnDetails sortColumn) {

    if (sortColumn.name == 'name') {

      return value1.name.toLowerCase().compareTo(value2.name.toLowerCase());

    }

    return super.compare(value1, value2, sortColumn);

  }

}

As we mentioned, we will include this changes in our upcoming weekly pub release. Please let us know if you have any concerns.

Regards,

Neelakandan



CS Chris Sells October 9, 2020 04:30 PM UTC

that looks *much* more sane. thank you!


NK Neelakandan Kannan Syncfusion Team October 12, 2020 07:35 AM UTC

Hi Chris,

You are welcome. As we mentioned, we will include the changes in our upcoming weekly pub release.


NK Neelakandan Kannan Syncfusion Team October 14, 2020 01:21 PM UTC

Hi Chris,

Thanks for your patience.

We have included the changes in our weekly pub release. You can now override the compare method in DataGridSource class. Please use the below version 18.3.40-beta version. 

Please let us know if you need further assistance on this.

Marked as answer

CS Chris Sells October 19, 2020 06:36 AM UTC

Thanks. That did the trick.


NK Neelakandan Kannan Syncfusion Team October 19, 2020 09:07 AM UTC

Hi Chris,

Thanks for the update. We glad to know that your requirement has been achieved at your end. Please let us know if you need further assistance from us. 


DO David OGUNDEPO January 17, 2023 10:18 AM UTC

Hello Kannan, can I use your assistance with the Datagrid widget?



SP Sangavi Periyannan Syncfusion Team January 17, 2023 12:48 PM UTC

David,

Please include more details about your query.  This would be helpful for us to serve you.


Loader.
Up arrow icon