0

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 ?

Antikhippe
  • 6,316
  • 2
  • 28
  • 43
Ayushi Tomar
  • 235
  • 1
  • 3
  • 17

1 Answers1

1

@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]
 }
Eliseo
  • 50,109
  • 4
  • 29
  • 67