How custom sortFunction in DataTable should works?

UI Components for React
Post Reply
Arnack
Posts: 1
Joined: 27 Aug 2019, 15:48

27 Aug 2019, 16:09

What should it does? I tried to return new value array, but it doesn't work.

mert.sincan
Posts: 5281
Joined: 29 Jun 2013, 12:38

09 Sep 2019, 16:58

You can do a new sorting implementation according to your needs. Please see;

Code: Select all

onSort(event) {
        let data = [...this.state.cars];
        data.sort((data1, data2) => {
            const value1 = data1[event.field];
            const value2 = data2[event.field];
            let result = null;

            if (value1 == null && value2 != null)
                result = -1;
            else if (value1 != null && value2 == null)
                result = 1;
            else if (value1 == null && value2 == null)
                result = 0;
            else if (typeof value1 === 'string' && typeof value2 === 'string')
                result = value1.localeCompare(value2, undefined, { numeric: true });
            else
                result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0;

            return (event.order * result);
        });

        return data;
    }
    ...
    <DataTable value={this.state.cars}>
                        <Column field="vin" header="Vin" sortable={true} sortFunction={this.onSort}/>
                        <Column field="year" header="Year" />
                        <Column field="brand" header="Brand" />
                        <Column field="color" header="Color" />
                    </DataTable>

Post Reply

Return to “PrimeReact”

  • Information
  • Who is online

    Users browsing this forum: Phillipdum and 8 guests