【问题标题】:How to sort data in a table data structure in Java?如何在 Java 中对表数据结构中的数据进行排序?
【发布时间】:2010-12-26 03:35:55
【问题描述】:

我需要根据表数据结构的第三列对数据进行排序。我根据以下question 的答案进行了尝试。但是我的排序不起作用。请帮助我。 这是我的代码。

Object[] data = new Object[y];
            rst.beforeFirst();
            while (rst.next()) {
                int p_id = Integer.parseInt(rst.getString(1));
                String sw2 = "select sum(quantity) from tbl_order_detail where product_id=" + p_id;
                rst1 = stmt1.executeQuery(sw2);
                rst1.next();
                String sw3 = "select max(order_date) from tbl_order where tbl_order.`Order_ID` in (select tbl_order_detail.`Order_ID` from tbl_order_detail where product_id=" + p_id + ")";
                rst2 = stmt2.executeQuery(sw3);
                rst2.next();
                data[i] = new Object[]{new String(rst.getString(2)), new String(rst.getString(3)), new Integer(rst1.getString(1)), new String(rst2.getString(1))};
                i++;
            }
            ColumnComparator cc = new ColumnComparator(2);
            Arrays.sort(data, cc);
            if (i == 0) {
                table.addCell("");
                table.addCell("");
                table.addCell("");
                table.addCell("");
            } else {
                for (int j = 0; j < y; j++) {
                    Object[] theRow = (Object[]) data[j];
                    table.addCell((String) theRow[0]);
                    table.addCell((String) theRow[1]);
                    table.addCell((String) theRow[2]);
                    table.addCell((String) theRow[3]);
                }

样本预期输出:

Product_code  Product_name  Quantity Order_date  
FK            Cake          3000      2010-12-09  
CK            Jelly         100      2010-09-23  
F             juice         30       2010-12-09  

但我得到的是:

Product_code  Product_name  Quantity Order_date  
CK            Jelly         100      2010-09-23  
F             juice         30       2010-12-09  
FK            Cake          3000      2010-12-09  

【问题讨论】:

  • 我很好奇你为什么不能让数据库为你做排序。
  • 能否为您的表格数据提供实际和预期的输出样本?

标签: java sorting data-structures


【解决方案1】:

你在这里做的太多了。您将数据库访问和 UI 混合到一个方法中。我会把这些问题分开。

我还建议让数据库进行排序。将 ORDER BY 添加到 SELECT 并让数据库完成工作。

我会将 SELECT 中的数据映射到具有 Comparator 用于排序的对象中。将 ResultSet 加载到该对象的列表中;这样你就可以满足你的所有愿望。

【讨论】:

  • 我觉得写一个复杂的查询很难。您能否检查查询并提供一个符合我要求的查询?
  • 我建议您从帖子中删除代码并指定要从数据库中检索的内容并提供表结构。
【解决方案2】:
  1. 问题出在数据还是比较器上?在另一篇文章中,您展示了如何使用硬编码数据创建一个简单的测试程序。您在此处发布的代码对我们没有帮助,因为我们无权访问您的数据库,也不知道您是否正确访问了数据。

  2. 输出看起来像是按“字符串”值升序排序的。所以看起来数据确实是错误的。我不知道问题出在哪里,因为看起来您正在向数组添加一个整数值。

  3. 您希望输出按金额降序排列,因此您需要设置一个 Comparator 属性来执行此操作。

无论如何,为了确保问题不在于我的 Comparator,我创建了一个简单的测试:

import java.util.*;

public class SortSIJ
{
    public static void main(String args[])
    {
        Object[] data = new Object[3];
        data[0] = new Object[] {"CK",  "Jelly", new Integer(100), "2010-09-23"};
        data[1] = new Object[] {"FK", "Cake", new Integer(3000), "2010-12-09"};
        data[2] = new Object[] {"F", "juice", new Integer(30), "2010-12-09"};

        ColumnComparator cc = new ColumnComparator(2);
        cc.setAscending( false );

        Arrays.sort(data, cc);

        for (Object row: data)
        {
            Object[] theRow = (Object[])row;
            System.out.println( Arrays.asList(theRow) );
        }
    }
}

输出对我来说看起来不错。我只能建议您修改 ColumnComparator 以添加以下代码行来验证正在排序的 Object 类型。

System.out.println(o1.getClass());

当我这样做时,我得到以下输出:

class java.lang.Integer  
class java.lang.Integer  
[FK, Cake, 3000, 2010-12-09]  
[CK, Jelly, 100, 2010-09-23]  
[F, juice, 30, 2010-12-09]  

【讨论】:

    猜你喜欢
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2015-03-08
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多