1

I need to sort data based on the third column of the table data structure. I tried based on the answers for the following question. But my sorting does not work. Please help me in this. Here goes my code.

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]);
                }

Sample Expected Output:

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  

but what I get is:

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  
2
  • 1
    I am curious why you cant let the database do the sorting for you. Commented Dec 26, 2010 at 3:40
  • Can you give actual and expected output samples for you table data, please? Commented Dec 26, 2010 at 3:43

2 Answers 2

4

You have far too much going on here. You're mingling database access and UI all into a single method. I'd separate those concerns.

I'd also recommend having the database do the sorting. Add an ORDER BY to the SELECT and let the database do the work.

I'd map the data from the SELECT into an object that had a Comparator for sorting. Load the ResultSet into a List of that object; you can have all your wishes that way.

Sign up to request clarification or add additional context in comments.

2 Comments

I feel it difficult to write a complicated query. Can you please check the queries and provide a single query that would meet my requirement?
I'd suggest you to remove the code from your post and specify WHAT you want to retrieve from the database and provide the table structures.
1
  1. Is the problem the data or the comparator? In the other posting you where shown how to create a simple test program using hard coded data. The code you posted here doesn't help us because we don't have access to your database and we don't know if you are accessing the data correctly.

  2. The output looks like it is sorted in ascending order by "String" value. So it does indeed look like the data is wrong. I don't know what the problem is since it looks like you are adding an Integer value to the array.

  3. You want the output in descending order by amount, so you need to set a Comparator property to do this.

Anyway to make sure the problem wasn't with my Comparator I created a simple test:

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) );
        }
    }
}

The output looks fine to me. All I can suggest is that you modify the ColumnComparator to add the following line of code to verify the Object type that is being sorted.

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

When I do that I get the following output:

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]  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.