Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to sort data in coulmns

Status
Not open for further replies.

sheed

Programmer
Jun 14, 2005
38
0
0
US
Hi,
How do I sort data IN Ascending or Descending order. Following is a sample code. I want to sort on Amount i.e. float OR Value i.e. integer OR Name i.e. String. I am showing data in my app in table and want to provide a functionality if a user clicks on any column header it sorts the whole data according. How can I make sorting work on all my columns. Here is code excerpt that is storing the data:

Code:
.....................................
.......................................
Test t = null;
ArrayList a = new ArrayList();

t = new Test();
t.setAmount(23.89);
t.setVal(1);
t.setName("TestName1");
a.add(t);

t = new Test();
t.setAmount(54.00);
t.setVal(2);
t.setName("Name2");
a.add(t);

t = new Test();
t.setAmount(55.89);
t.setVal(3);
t.setName("Doe");
a.add(t);
..........................
.........................

And following is my jsp code:

Code:
.....................................
.......................................
ArrayList alist = (ArrayList)session.getAttribute("DataRecords");
 Collections.sort(alist);
 Comparator comp = Collections.reverseOrder();
 Collections.sort(alist,comp);
    ..........................
.........................

Currently I am just sorting on Amount i.e. user clicks the Amount column to sort, how can I make the sort work for other columns i.e. if user clicks on Name column or Value column it sorts all the data.

Any help is appreciated.

Thanks
 
You will have to implement your own Comparator to compare Test object based on different "sort-by" column.
 
Here is my class that implementing Comparable. But currently I am just comparing the Amount how can I make to compare Val and Name also.

Code:
public class DataSort implements Comparable {
    
    private double cAmount;
    private int cVal;
    private String cName;   
    
    public double getcAmount() 
    {
        return cAmount;
    }

    public void setcAmount(String cAmount)
    {
        this.cAmount = Double.parseDouble(cAmount);
    }
    
    public int getcVal()
    {
        return cVal;
    }

    public void setcVal(String cVal)
    {
        this.cVal = Integer.parseInt(cVal);
    }
    
    public String getcName()
    {
        return cName;
    }

    public void cName(String cName)
    {
        this.cName = cName;
    }
            
    public int compareTo(Object d) throws ClassCastException 
    {
        if (!(d instanceof DataSort))
            throw new ClassCastException("A DataSort object expected.....");
        Double aAmount = new Double(((DataSort) d).getcAmount());
        Double tAmount = new Double(this.cAmount);
        return tAmount.compareTo(aAmount);
    }
}
 
Hi,

Code:
import java.util.Comparator;

/**
 * User: vreddy
 * Date: Jun 21, 2005
 * Time: 6:00:33 PM
 *
 * @author Venu Reddy 
 */
public class DataSort implements Comparable {

    private double cAmount;
    private int cVal;
    private String cName;

    public double getcAmount() {
        return cAmount;
    }

    public void setcAmount(String cAmount) {
        this.cAmount = Double.parseDouble(cAmount);
    }

    public int getcVal() {
        return cVal;
    }

    public void setcVal(String cVal) {
        this.cVal = Integer.parseInt(cVal);
    }

    public String getcName() {
        return cName;
    }

    public void cName(String cName) {
        this.cName = cName;
    }

    public int compareTo(Object d) throws ClassCastException {
        if (!(d instanceof DataSort))
            throw new ClassCastException("A DataSort object expected.....");
        Double aAmount = new Double(((DataSort) d).getcAmount());
        Double tAmount = new Double(this.cAmount);
        return tAmount.compareTo(aAmount);
    }

    public static Comparator NameComparator = new Comparator() {
        public int compare(Object newTestBean1, Object newTestBean2) {
            String name1 = ((DataSort) newTestBean1).getcName().toUpperCase();
            String name2 = ((DataSort) newTestBean2).getcName().toUpperCase();
            return name1.compareTo(name2);
        }
    };

    public static Comparator ValComparator = new Comparator() {
        public int compare(Object newTestBean1, Object newTestBean2) {
            Integer val1 = new Integer(((DataSort) newTestBean1).getcVal());
            Integer val2 = new Integer(((DataSort) newTestBean1).getcVal());
            return val1.compareTo(val2);
        }
    };

}

Test
Code:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.util.Comparator;

/**
 * User: vreddy
 * Date: Jun 21, 2005
 * Time: 6:04:33 PM
 *
 * @author Venu Reddy <a href="mailto:venu_dvmr@yahoo.com">Venu Reddy</a>
 */
public class TestBeanTest {

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        DataSort test = new DataSort();
        test.setcAmount("10");
        test.setcVal("1");
        test.cName("Apple");
        list.add(test);
        test = new DataSort();
        test.setcAmount("10");
        test.setcVal("3");
        test.cName("Cat");
        list.add(test);
        test = new DataSort();
        test.setcAmount("10.12");
        test.setcVal("2");
        test.cName("Boy");
        list.add(test);
        test = new DataSort();
        test.setcAmount("10.15");
        test.setcVal("5");
        test.cName("Zero");
        list.add(test);
        test = new DataSort();
        test.setcAmount("10.14");
        test.setcVal("4");
        test.cName("Dog");
        list.add(test);

        System.out.println("\n");
        System.out.println("*** Orginal List ***");
        printValues(list);
        System.out.println("\n");
        // sort the list
        Collections.sort(list);
        System.out.println("*** Sort By Amount ASC***");
        printValues(list);
        System.out.println("\n");
        System.out.println("*** Reverse Sort By Amount DESC***");
        Comparator comp = Collections.reverseOrder();
        Collections.sort(list,comp);
        printValues(list);

        System.out.println("\n");
        System.out.println("*** Sort By Name ASC***");
        Collections.sort(list, DataSort.NameComparator);
        printValues(list);
        System.out.println("\n");
        System.out.println("*** Reverse Sort By Name DESC***");
        Collections.reverse(list);
        printValues(list);

        System.out.println("\n");
        System.out.println("*** Sort By Val ASC***");
        Collections.sort(list, DataSort.ValComparator);
        printValues(list);
        System.out.println("\n");
        System.out.println("*** Reverse Sort By Val DESC***");
        Collections.reverse(list);
        printValues(list);

    }

    public static void printValues(ArrayList list)
    {
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            DataSort testBean = (DataSort)iterator.next();
            System.out.println("$" + testBean.getcAmount() + ", " + testBean.getcName() + ", " +
                    ""+ testBean.getcVal()+ "");
        }
    }
}

Output
Code:
*** Orginal List ***
$10.0, Apple, 1
$10.0, Cat, 3
$10.12, Boy, 2
$10.15, Zero, 5
$10.14, Dog, 4


*** Sort By Amount ASC***
$10.0, Apple, 1
$10.0, Cat, 3
$10.12, Boy, 2
$10.14, Dog, 4
$10.15, Zero, 5


*** Reverse Sort By Amount DESC***
$10.15, Zero, 5
$10.14, Dog, 4
$10.12, Boy, 2
$10.0, Apple, 1
$10.0, Cat, 3


*** Sort By Name ASC***
$10.0, Apple, 1
$10.12, Boy, 2
$10.0, Cat, 3
$10.14, Dog, 4
$10.15, Zero, 5


*** Reverse Sort By Name DESC***
$10.15, Zero, 5
$10.14, Dog, 4
$10.0, Cat, 3
$10.12, Boy, 2
$10.0, Apple, 1


*** Sort By Val ASC***
$10.15, Zero, 5
$10.14, Dog, 4
$10.0, Cat, 3
$10.12, Boy, 2
$10.0, Apple, 1


*** Reverse Sort By Val DESC***
$10.0, Apple, 1
$10.12, Boy, 2
$10.0, Cat, 3
$10.14, Dog, 4
$10.15, Zero, 5

If you are using commons-beanutils.jar and commons-collections.jar in your classpath it will be much simpler and cleaner.

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top