Search code examples
angularprimengprimeng-turbotable

Unable to sort on turbo table column in 'dd/mmm/yyyy' while using primeng and angular 5


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 ?


Solution

  • @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]
     }