如何在表中查找列的重复值并跨列执行排序



我在给定的表中进行排序,我要求根据排序隐藏和显示值。例如:如果用户正在单击(Credit列,则outstandingallbal的列值对于ASCDESC都应为null(,则工作正常。我想执行排序,就像如果(Debit列的值相同,我想根据">Date"对借方栏进行排序(AscDesc记"以及">日期";列是相同的,我想根据"对Debit列进行排序访问者代码";(Asc

Desc例如,在"借方列"中,如果我有5个值(100、100、100和200、300(,我希望根据"日期"列显示"借方"列的值。如果日期和借方列相同例如:日期列有(2021-02-042021-02-1021-02-082021-02-08((YYYY-MM-DD(借方列(100100100200300(我想根据"日期"对借方列进行排序访问者代码";。

我已经尝试了很多方法来实现这一点,不幸的是,我所有的逻辑都失败了。任何人都可以指导我如何进行上述排序。提前谢谢。

以下是我的代码:

const props = {
"rows" : 
[{
"visitiedDate": "2021-04-02",
"visitorCode": [
"ABC001"
], 
"visittype": 1,
"debit": 800,
"credit": 100,
"oustanding": 1000,
"allbal":1000
},
{
"visitiedDate": "2021-04-02",
"visitorCode": [
"ABC002"
], 
"visittype": 1,
"debit": 800,
"credit": 100,
"oustanding": 1000,
"allbal":1000
},
{
"visitiedDate": "2021-02-02",
"visitorCode": [
"ABC003"
], 
"visittype": 2,
"debit": 900,
"credit": 100,
"oustanding": 1000,
"allbal":1000
},
{
"visitiedDate": "2021-03-01",
"visitorCode": [
"ABC004"
], 
"visittype": 2,
"debit": 1000,
"credit": 100,
"oustanding": 1000,
"allbal":1000
},
{
"visitiedDate": "2021-04-02",
"visitorCode": [
"ABC005"
], 
"visittype": 1,
"debit": 500,
"credit": 100,
"oustanding": 1000,
"allbal":1000
}], 

columns = [{
"key": "date",
"name": "Date",
"isSort": true
},

{
"key": "visittype",
"name": "type",
"isSort": true
},

{
"key": "vistiorCode",
"name": "code",
"isSort": false
},

{
"key": "credit",
"name": "camount",
"isSort": true
},

{
"key": "debit",
"name": "damount",
"isSort": true
},
{
"key": "outstanding",
"name": "outstandingBal",
},

{
"key": "allbal",
"name": "allBalance",
},
]
const [col, setCol] = useState(columns)
const Rows = props.contractDetails; 
const TotalDetails = props.totalCount
function SortingRow(rows, col, sortMethod) {
const sortRow = ((sortMethod === "ASC") || ((col === "type") || (col === "credit") || (col === "debit"))?
rows.map(Details) => {
if (col === "debit")
return  Object.assign({}, Details, {outstanding: "", allbal: ""})   
else  if(col === "credit"){
return  Object.assign({}, Details, {outstanding: "", allbal: ""})   
})
: rows
return sortMethod ?
sortRow
.slice()
.sort(
({
[col]: a
}, {
[col]: b
}) =>
(a === b ? 0 : a < b ? -1 : 1) *
(sortMethod === "ASC" ? 1 : -1)
}) :
rows
}
const rows = col
.slice()
.reverse()
.reduce(
(sortedRows, {
key: col,
isSort,
sortMethod
}) =>
isSort ? SortingRow(sortedRows, col, sortMethod) : sortedRows,
Rows
);
return (
rows = {
rows
}
col = {
col
}
onSort = {
(col, sortMethod) => setCol(
cols.map(cl =>
c.key === col ? Object.assign({}, cl, {
sortMethod
}) :
cl
)
)
}
)

} 

您可以通过使用Array#sort()方法来实现这一点:

let rows = [
{
visitiedDate: "2021-04-02",
visitorCode: ["ABC001"],
visittype: 1,
debit: 800,
credit: 100,
oustanding: 1000,
allbal: 1000
},
{
visitiedDate: "2021-04-02",
visitorCode: ["ABC002"],
visittype: 1,
debit: 800,
credit: 100,
oustanding: 1000,
allbal: 1000
},
{
visitiedDate: "2021-02-02",
visitorCode: ["ABC003"],
visittype: 2,
debit: 900,
credit: 100,
oustanding: 1000,
allbal: 1000
},
{
visitiedDate: "2021-03-01",
visitorCode: ["ABC004"],
visittype: 2,
debit: 1000,
credit: 100,
oustanding: 1000,
allbal: 1000
},
{
visitiedDate: "2021-04-02",
visitorCode: ["ABC005"],
visittype: 1,
debit: 500,
credit: 100,
oustanding: 1000,
allbal: 1000
}
];
console.log(
rows.sort((a, b) =>
a.debit < b.debit
? -1
: 0 && new Date(a.visitiedDate) < new Date(b.visitiedDate)
? -1
: 0 || (a.visitorCode[0] < b.visitorCode[0] ? -1 : 1)
)
);

最新更新