简体   繁体   English

单击时按降序和升序排序

[英]Sort descending and ascending on click

I am sorting a table and when I click, the order goes to ascending but not descending.我正在对表格进行排序,当我单击时,顺序会升序而不是降序。 I have a boolean value that should toggle true or false but it keeps logging out the initial value.我有一个布尔值,应该切换真或假,但它会不断注销初始值。 Can someone help me understand why the if statement isn't working?有人可以帮助我理解为什么 if 语句不起作用吗? Also when I sort how do I return to the original array?另外,当我排序时如何返回到原始数组?

 let sortDirection = false;
    const sortTable = (key) => {
        sortDirection = !sortDirection
        const clonedOptions = [...listOfOptions];
        clonedOptions.sort((a, b) => {
            return sortDirection ? a[key] - b[key] : b[key] - a[key];
        })
        setListOfOptions(clonedOptions);
    }

 <div className="outputs" >
                <table>
                    <thead>
                        <tr>
                            <th></th>
                            <th onClick={() => sortTable('clock')}>Date </th>
                            <th onClick={() => sortTable('name')}>Stock Name</th>
                            <th onClick={() => sortTable('price')}>Price Of Option</th>
                            <th onClick={() => sortTable('amountOfOptions')}>Number Of Options</th>
                            <th onClick={() => sortTable('totalAmountSpent')}>Total Amount Spent</th>
                            <th onClick={() => sortTable('optionPriceSoldAt')}>Option Sold At</th>
                            <th onClick={() => sortTable('amountOfOptionsSold')}>Amount Of Options Sold</th>
                            <th onClick={() => sortTable('totalProfit')}>Proft</th>
                        </tr>
                    </thead>
                    {listOfOptions.map((option) => {
                        return (
                            <tbody key={uuidv1()}>
                                <tr>
                                    <td title="delete" onClick={() => deleteOption(option.id)}><span className="delete">x</span></td>
                                    <td>{option.clock}</td>
                                    <td>{option.name.toUpperCase()}</td>
                                    <td>${option.price}</td>
                                    <td>{option.amountOfOptions}</td>
                                    <td>${option.totalAmountSpent.toFixed(2)}</td>
                                    <td>${option.optionPriceSoldAt}</td>
                                    <td>{option.amountOfOptionsSold}</td>
                                    <td style={{ color: option.totalProfit >= 0 ? 'green' : 'red' }}>${option.totalProfit.toFixed(2)}</td>
                                </tr>
                            </tbody>
                        )
                    })}
                </table>
            </div>
        </div>

You need to put the boolean into state , otherwise, on each render, it'll start out as false :您需要将布尔值放入state ,否则,在每次渲染时,它都会以false开始:

const [sortDirection, setSortDirection] = useState(false);
const sortTable = (key) => {
    setSortDirection(!sortDirection);
    const clonedOptions = [...listOfOptions];
    clonedOptions.sort((a, b) => {
        return sortDirection ? b[key] - a[key] : a[key] - b[key];
    })
    setListOfOptions(clonedOptions);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM