Sorting datatables by length of string


Topic: Sorting datatables by length of string

Elisa Straatman asked a year ago

Hi,

I've made a datatable which displays the amount of likes a user has.I live in Europe and really wanted to have thousands separators. I used toLocaleString() for this. What has happened is that the table now sorts all the data like a string instead of a number, which makes sense. I couldn't find any way to format a number like this, whilst remaining a number. Is there any way to sort a column depending on the length of a string? I’m looking forward to the replies :).

Code for adding the separators:

componentDidMount() {
    //The data to be used for the tables
    let transformedData = [...importData.influencers];
    const newData = transformedData.map(obj => {
      if (obj.influencerId)
        return {
          ...obj,
          totalLikes: (
            obj.totalLikes.toLocaleString()
          )
        };

      return obj;
    });
    this.setState({ tableData: newData });
  }

Code used for the table sorting:

 <MDBDataTable
                data={data}
                order={["totalLikes", "desc"]}
              />

Screenshot: enter image description here


Konrad Stępień staff answered a year ago

Hi @Elisa Straatman,

If you wanna sort Likes it should by type of number, not string.

I did not find the function what you expect. But if you want we can try to update DataTable to sort string like numbers.

Are you interested in this option?

Best, Konrad.


Elisa Straatman commented a year ago

That would be great!


Please insert min. 20 characters.
Status

Answered

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: React
  • MDB Version: 4.17.0
  • Device: PC
  • Browser: Chrome
  • OS: Windows
  • Provided sample code: No
  • Provided link: No