link 上的反向 ArrayList

Reverse ArrayList on link

我有一个 ArrayList,它由名为“History”的自定义对象组成。当我单击代码示例顶部的 link,即名为“Deposit1”的那个时,我会以正确的顺序显示列表。

但是,我希望能够在用户单击 table 中的 link “Deposit2” 时反转列表,只有在用户单击第一个 link, "存款1".

现在什么都没发生,但我可以 system.out.print 成功地点击第二 link。

有办法吗?

请注意,为了更好地概述,代码示例已被删减。

ArrayList <history> history = historyDAO.getHistory(x, y);



<li>
     <a href="history.jsp?Dep1=valueOfDep">Deposit1</a>
</li>

String sortDep1 = request.getParameter("Dep1"); 
String sortDep2 = request.getParameter("Dep2");

if (sortDep1 != null) {         
    
    <table class="table text">
      <tr>
        <th>Result</th>
        <th>Sport</th>
        <th>Deposit2<a href="history.jsp?Dep2=valueOfDep">Deposit2</a></th>
      </tr>

 if (sortDep2 != null) {
    Collections.reverse(history);
   }   


for (int i = 0; i < history.size() && i < list1.size(); i++) {
     
    <tr>
            <td><%=history.get(i).getRes()%></td>
            <td><%=history.get(i).getSport() %></td>
            <td><%=history.get(i).getDeposit()%></td>
    </tr>
  
<%}}%>

</tr>
</table>

将第一个if-sortDep1改为:

if (sortDep1 != null || sortDep2 != null) {

那么这部分代码总是在任何一个有效时输入。

您可以在 Dept1Dept2 的 href 标记中传递 2 参数,这样 sortDep1 就不会为 null,它会进入 if-statement 并根据 url 中的值,您可以反转列表。即:

if (sortDep1 != null) {         
<table class="table text">
<tr>
   <th>Result</th>
   <th>Sport</th>
   //pass 2 parameter in url for dep1 & dep2
   <th>Deposit2<a href="history.jsp?Dep1=valueOfDep&Dep2=reverse">Deposit2</a></th>
</tr>
//check if not null and value is reverse
  if ((sortDep2 != null) && (sortDep2.equals("reverse")) {
    Collections.reverse(history);
    }   
 //other codes