0
votes

I am having a requirement where I want to sort the table on the basis of the headers of column.

I have used PrimeNG turbotable sorting for this purpose, everthing works fine but when I try to sort the column which contains the date in the format 'dd-mmm-yyyy' (12-Nov-2016) it does not sort on the basis of month it just takes the dd and sort it accordingly.

I am using it with angular 5

Link from i took the code

Can someone please help here ?

1
You need a custom sort when you work on dates. First transform the date to a string in format yyyy-mm-dd and compare string or a object Date and compare getTime - Eliseo
i can't change the format, this is one of the business requirement - Ayushi Tomar
is in your customSort, not the data - Eliseo

1 Answers

1
votes

@Ayushi, is in your customSort

customSort(event: SortEvent) {
        event.data.sort((data1, data2) => {

            let value1Data = data1[event.field];
            let value2Data = data2[event.field];

            let value1=this.transform(value1Data); //<--this
            let value2=this.transform(value2Data); //<--this
            ....
       }
}
//make a const array of meses
const meses:string[]=["Ene","Feb","Mar","Abr","May","Jun",
           "Jul","Ago","Sep","Oct","Nov","Dic"];
//function transform
transform(value:string)
{
     let step:string[]=value.split('-'); //separate the date in [dd,mmm,yyyy]
     //month will be 01,02,03..12
     let month=meses.indexOf(step[1])<9?'0'+(meses.indexOf(step[1])+1):''+(meses.indexOf(step[1])+1)
     return step[2]+'-'+month+'-'+step[0]
 }