在反应中将数字转换为字符串进行排序
Sorting numbers cast as strings in react
背景
我的数据包含“1,000”和“123,000”之类的字符串,我想将它们排序为 material-table。
我希望它们从数字角度而不是字符串角度按值排序。
问题
当我仍然希望单元格包含字符串格式允许的逗号时,如何按数字值而不是字符串字母顺序对单元格进行排序。
到目前为止的代码
我正在尝试在此处进行薪资排序:
function CustomFilteringAlgorithm() {
return (
<MaterialTable
title="Custom Filtering Algorithm Preview"
columns={[
{
title: 'Name',
field: 'name',
customSort: (a, b) => a.name.length - b.name.length
},
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{ title: 'Salary', field: 'salary', type: 'numeric',
customSort: (a, b) => parseInt(a.salary) - parseInt(b.salary),
},
{
title: 'Birth Place',
field: 'birthCity',
lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
},
]}
data={[
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63, salary: '123,000' },
{ name: 'Joe', surname: 'Baran', birthYear: 1947, birthCity: 63, salary: '1,000' },
{ name: 'John', surname: 'Smith', birthYear: 1988, birthCity: 63, salary: '2,000' },
{ name: 'Jaun', surname: 'Smittle', birthYear: 1988, birthCity: 63, salary: '4,000' },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34, salary: '45,000' },
{ name: 'Vishal', surname: 'Miller', birthYear: 2000, birthCity: 34, salary: '15,000,000' },
]}
options={{
sorting: true
}}
/>
)
}
备注
可以使用 this page 上的自定义排序部分来测试代码:
试试这个,
首先用于排序转换为数字
const salary = Number(('123,000').replace(',', ''));
那么,
const salaryString = salary.toLocaleString('en');
parseInt()
不适用于带逗号的字符串,您必须在排序时手动删除它们:
customSort: (a, b) => parseInt(a.salary.replace(/,/g, '')) - parseInt(b.salary.replace(/,/g, ''))
背景
我的数据包含“1,000”和“123,000”之类的字符串,我想将它们排序为 material-table。 我希望它们从数字角度而不是字符串角度按值排序。
问题
当我仍然希望单元格包含字符串格式允许的逗号时,如何按数字值而不是字符串字母顺序对单元格进行排序。
到目前为止的代码
我正在尝试在此处进行薪资排序:
function CustomFilteringAlgorithm() {
return (
<MaterialTable
title="Custom Filtering Algorithm Preview"
columns={[
{
title: 'Name',
field: 'name',
customSort: (a, b) => a.name.length - b.name.length
},
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{ title: 'Salary', field: 'salary', type: 'numeric',
customSort: (a, b) => parseInt(a.salary) - parseInt(b.salary),
},
{
title: 'Birth Place',
field: 'birthCity',
lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
},
]}
data={[
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63, salary: '123,000' },
{ name: 'Joe', surname: 'Baran', birthYear: 1947, birthCity: 63, salary: '1,000' },
{ name: 'John', surname: 'Smith', birthYear: 1988, birthCity: 63, salary: '2,000' },
{ name: 'Jaun', surname: 'Smittle', birthYear: 1988, birthCity: 63, salary: '4,000' },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34, salary: '45,000' },
{ name: 'Vishal', surname: 'Miller', birthYear: 2000, birthCity: 34, salary: '15,000,000' },
]}
options={{
sorting: true
}}
/>
)
}
备注
可以使用 this page 上的自定义排序部分来测试代码:
试试这个, 首先用于排序转换为数字
const salary = Number(('123,000').replace(',', ''));
那么,
const salaryString = salary.toLocaleString('en');
parseInt()
不适用于带逗号的字符串,您必须在排序时手动删除它们:
customSort: (a, b) => parseInt(a.salary.replace(/,/g, '')) - parseInt(b.salary.replace(/,/g, ''))