1

I am using primeNg <p-dataTable> to implement custom sort for "Row No:" field.

The values in the row number field will be as below:

9864-3
4738-1
0935-4
2788-2

Source : https://primefaces.org/primeng/#/table/sort

To sort the data I have done below code:

HTML:

<p-dataTable  (onSort)="changeSort($event)">
        <p-column field="rowNo" header="Row No:" [sortable]="true">{{value}}</p-column>
</p-dataTable>

TS:

changeSort(event) {
        if (!event.order) {
          this.sortF = 'year';
        } else {
          this.sortF = event.field;
        }
    }

But this is not sorting is not happening, am not getting any error and neither is the data getting sorted. Please guide me how to sort data which contains - in it?

Anna
  • 1,669
  • 7
  • 38
  • 63

1 Answers1

0

I tried to make a very basic custom sort function which sorts according to your specification

Basically you can sort list of number like arr.sort((a,b) => parseInt(a) - parseInt(b)). But here what the problem is parseInt would deduct the number as 9864-3 would result in 9861 and then deduct to the next number and gives result.

Anyways the custom function is:

data = ["0935-4", "0935-4", "0935-2", "4738-1", "9864-3"];

customSort(data) {
 let arr = [];
 data.map(c => {
 let index = c.indexOf('-');
 let d = c.split('');
 d.splice(index, 1);
 arr.push(d.join(''))
 })
 arr.sort((a,b) => parseInt(a) - parseInt(b))// ascending
 // arr.sort((a,b) => parseInt(b) - parseInt(a)) descending
 let temp2 = [];
 arr.map(val => {
  temp2.push(val.slice(0, 4) + "-" + val.slice(4));
 })
  this.data = [...temp2];
  console.log(this.data)
}

Hope this might help you or give you some idea to make it to your own

Suryan
  • 701
  • 7
  • 17