Skip to content Skip to sidebar Skip to footer

Why Sorting Is Not Working On Some Of The Columns?

I have used column sorting on table. It's working fine on first name and last name column but on dl and dl score it is not working. Can you look into it and help me fix it. You can

Solution 1:

You declare your sort method like:

 this.allUser.sort(function(
      a: { [x: string]: number },
      b: { [x: string]: number }
    )

You're saying that all objects in your array has keys that all return numbers.

So when you sort on first_name prop it finds values like A Male when doing a['first_name'], which is not a number at all, so clearly that's a logical problem. The sorting works for it in runtime anyway since javascript can sort strings like this.

But when you sort on dl, that property does not even exist on your objects, so it's comparing undefined with undefined (a['dl']), which are equal so no sorting will happen.

So ensure to populate your objects with a value for property dl if you want to sort on it.

Post a Comment for "Why Sorting Is Not Working On Some Of The Columns?"