Topic: Sorting datatables by length of string

Elisa Straatman free asked 5 years 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 5 years 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 free commented 5 years ago

That would be great!



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB React
  • MDB Version: 4.17.0
  • Device: PC
  • Browser: Chrome
  • OS: Windows
  • Provided sample code: No
  • Provided link: No