2

I have implemented the ng2 smart table grid for listing

https://akveo.github.io/ng2-smart-table/#/documentation

Here I want to integrate the sorting for 2 columns. For this I used the code

 storeid: {
  title: 'Store #',
  sort: true
},

But this is not numeric sorting. It sorts as 1,10,11... instead of 1,2,3... Is there any example for comparefunction for the particular column.

Please help me.

Sam Hanson
  • 1,317
  • 4
  • 22
  • 48

1 Answers1

0

I'm using the following function to sort a column with text and numbers in it:

compareFunction(direction: any, a: any, b: any) {

  let first = isNaN(Number(a)) ? a.toLowerCase() : Number(a);
  let second = isNaN(Number(b)) ? b.toLowerCase() : Number(b);

  if (first < second || (!isNaN(Number(a)) && isNaN(Number(b)))) {
    return -1 * direction;
  }
  if (first > second || (isNaN(Number(a)) && !isNaN(Number(b)))) {
    return direction;
  }


  return 0;
} 

It shows the numbers first (numeric sort) and then the text (1, 2, 10, a, b, c)

Lumix
  • 311
  • 1
  • 5