2

My data:

  data: [
  ["A","2"],
  ["B","100"],
  ]

I was expecting it to sort by amount: 2 then 100, but it's the opposite. First row shows 100, second row shows 2. Is there a way to sort by the actual amount rather than this numeric ordering?

Adam
  • 3,311
  • 1
  • 18
  • 9

2 Answers2

2
          new gridjs.Grid({
              columns: [
                  "Fecha",
                  {
                      name: "Cantidad",
                      sort: {
                          compare: (a, b) => {
                              if (parseFloat(a) > parseFloat(b)) {
                                  return 1;
                              } else if (parseFloat(b) > parseFloat(a)) {
                                  return -1;
                              } else {
                                  return 0;
                              }
                          },
                      },
                  },
                  "Tipo",
                  "Origen",
              ]
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Alessandro Benassi Dec 14 '22 at 11:28
1

As "2" and "100" are interpreted as string - the sort order is alphanumeric instead of numeric.

Try

data: [
  ["A",2],
  ["B",100],
]

instead to sort by the numbers.

Twinnie
  • 131
  • 4