Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
number formatting in antD tables, or localeCompare() number strings with commas

I have values I want to display in an AntD table. I want them to be sortable, but also formatted with commas, like "1,000". Ideally I would store them in React state as integers, and then format them as strings with commas upon display, but I haven't found a way in AntD tables to do this conversion last minute. Here's an example column JSX element displaying unformatted but sortable integers:

            <Column
              title="Population"
              dataIndex="population"
              key="population"
              sorter={(a, b) => a.population - b.population}
            />

I have tried converting all my state to formatted strings, and then the table displays them nicely, but the sorting is messed up. I thought localeCompare with numeric = True would fix this, but it doesn't.

With this input:

      let arr = ['999', '1,000', '1,001'];
      console.log(
        arr.sort((a, b) =>
          a.localeCompare(b, undefined, {
            numeric: true,
            ignorePunctuation: true,
          })
        )
      );

I expect the input to be sorted numerically, but it isn't. Perhaps I misunderstand how localeCompare() works. This is the result:

[ "1,000", "1,001", "999" ]

So I have two questions, and answering either one would fix my problem:

  1. Can I have an AntD table with columns that formats integers into a readable format on display, and if so, how?
  2. If not, how can I simply sort string representations of integers with commas (and $ amounts for that matter)? I could go regex here of course but I'm hoping there's a simpler solution I've missed. Changing integers into strings and then back to integers to sort them seems very silly.
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think the best approach in your case would be to use the render property of the Column.

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { Table } from "antd";

const { Column } = Table;

const data = [
  {
    key: "1",
    name: "John Brown",
    salary: 1000
  },
  {
    key: "2",
    name: "Jim Green",
    salary: 2000
  },
  {
    key: "3",
    name: "Joe Black",
    salary: 999
  },
  {
    key: "4",
    name: "Jim Red",
    salary: 1001
  }
];

ReactDOM.render(
  <Table dataSource={data}>
    <Column title="Name" dataIndex="name" key="name" />
    <Column
      title="Salary"
      dataIndex="salary"
      key="salary"
      sorter={(a, b) => a.salary - b.salary}
      render={(value) => {
        return "$" + value.toLocaleString("en");
      }}
    />
  </Table>,
  document.getElementById("container")
);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda