How custom sortFunction in DataTable should works?
-
- Posts: 5281
- Joined: 29 Jun 2013, 12:38
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>
Github Profile: https://github.com/mertsincan