NgxDeeboData Custom Sort
Custom Sort by column
You can import the CustomSortService and CustomSort interface into your parent component, then set a separate, custom sort for ascending and
descending order sorts.
Follow the imports and code below in your component
import { NgxDeebodata, CustomSortService, CustomSort } from 'ngx-deebodata';
In the exported class, call the customSortService.setCustomSort(obj) function where obj is of type CustomSort. The CustomSort interface
is an object with two parent properties - asc and desc. For a given column you want to sort yourself, you should create an object property for asc and desc
with that column name/key, and set it as a function that implements the ECMAscript custom sort callback like below. You need to access the field as part of the object
it belongs to in your main array of data like a[yourColumnName].
ngOnInit() {
this.setCustomSort()
this.testService.getData().subscribe({
next: (data: any) => this.data.set(data.result)
})
}
setCustomSort() {
const field = "category";
let obj: CustomSort = {
asc: {
category: (a: any, b: any) => {
if(!a[field] && a[field] !== 0)
return 1
if(!b[field] && b[field] !== 0)
return -1
if(a[field] > b[field])
return 1
if((a[field] === b[field]) || (JSON.stringify(a[field]) === JSON.stringify(b[field])))
return 0
return -1
}
},
desc: {
category: (a: any, b: any) => {
if(!a[field] && a[field] !== 0)
return 1
if(!b[field] && b[field] !== 0)
return -1
if(a[field] > b[field])
return -1
if((a[field] === b[field]) || (JSON.stringify(a[field]) === JSON.stringify(b[field])))
return 0
return 1
}
},
}
this.customSortService.setCustomSort(obj);
}