Sorting the Rows in a JTable Component Based on a Column

This example implements a method that sorts all the rows in a DefaultTableModel based on the values of the column.
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);

// Add data here...

// Disable autoCreateColumnsFromModel otherwise all the column customizations
// and adjustments will be lost when the model data is sorted
table.setAutoCreateColumnsFromModel(false);

// Sort all the rows in descending order based on the
// values in the second column of the model
sortAllRowsBy(model, 1, false);

// Regardless of sort order (ascending or descending), null values always appear last.
// colIndex specifies a column in model.
public void sortAllRowsBy(DefaultTableModel model, int colIndex, boolean ascending) {
    Vector data = model.getDataVector();
    Collections.sort(data, new ColumnSorter(colIndex, ascending));
    model.fireTableStructureChanged();
}

// This comparator is used to sort vectors of data
public class ColumnSorter implements Comparator {
    int colIndex;
    boolean ascending;
    ColumnSorter(int colIndex, boolean ascending) {
        this.colIndex = colIndex;
        this.ascending = ascending;
    }
    public int compare(Object a, Object b) {
        Vector v1 = (Vector)a;
        Vector v2 = (Vector)b;
        Object o1 = v1.get(colIndex);
        Object o2 = v2.get(colIndex);

        // Treat empty strains like nulls
        if (o1 instanceof String && ((String)o1).length() == 0) {
            o1 = null;
        }
        if (o2 instanceof String && ((String)o2).length() == 0) {
            o2 = null;
        }

        // Sort nulls so they appear last, regardless
        // of sort order
        if (o1 == null && o2 == null) {
            return 0;
        } else if (o1 == null) {
            return 1;
        } else if (o2 == null) {
            return -1;
        } else if (o1 instanceof Comparable) {
            if (ascending) {
                return ((Comparable)o1).compareTo(o2);
            } else {
                return ((Comparable)o2).compareTo(o1);
            }
        } else {
            if (ascending) {
                return o1.toString().compareTo(o2.toString());
            } else {
                return o2.toString().compareTo(o1.toString());
            }
        }
    }
}

Comments

27 May 2010 - 9:13pm by Alejandro (not verified)

Thanks a lot, it works perfect!!

5 Sep 2010 - 1:58pm by Dr.Vet. Cumpanasu Florin (not verified)

table.setAutoCreateRowSorter(true);

5 Sep 2010 - 1:59pm by Dr.Vet. Cumpanasu Florin (not verified)

table.setAutoCreateRowSorter(true);
java 1.6

13 Oct 2010 - 5:14pm by Vahid (not verified)

Thanks :)

16 Apr 2011 - 5:53am by rejith (not verified)

thankss :)

1 Sep 2011 - 11:30am by Anonymous (not verified)

table.setAutoCreateRowSorter(true); is good for sorting all columns but in cases where only specific columns are to be sorted, above example will help.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.