Duvida - Reordenação de Dados - JTable - QuickSort

0 respostas
orderdesenvolvimentojtablejava
4mega

Possuo uma classe que deveria reordenar toda a minha tabela, acredito que o método quicksort é funcional, porém ele n atualiza os valores ao jTable.

package classes;

/**
 *
 * @author clemerson.medeiros
 */
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public abstract class TableQuick extends AbstractTableModel {


    protected JTable table;
    protected boolean ascending = true;

    public TableQuick(JTable table) {
        super();
        setTable(table);
    }

    public TableQuick() {
        super();
    }

    public final void setTable(JTable table) {
        this.table = table;
    }

    public JTable getTable() {
        return table;
    }

    public int compareRowsByColumn(int row1, int row2, int column) {
        if (row2 < table.getRowCount()) {
            Class type = table.getColumnClass(
            Object o1 = table.getValueAt(row1, column);
            Object o2 = table.getValueAt(row2, column);
            if (o1 == null && o2 == null) {
                return 0;
            } else if (o1 == null) 
            {
                return -1;
            } else if (o2 == null) {
                return 1;
            }

            if (o1 instanceof java.lang.Number) {
                double d1 = ((Number) o1).doubleValue();
                double d2 = ((Number) o2).doubleValue();

                if (d1 < d2) {
                    return -1;
                } else if (d1 > d2) {
                    return 1;
                } else {
                    return 0;
                }
            } else if (type == java.util.Date.class) {
                long n1 = ((Date) o1).getTime();
                long n2 = ((Date) o2).getTime();

                if (n1 < n2) {
                    return -1;
                } else if (n1 > n2) {
                    return 1;
                } else {
                    return 0;
                }
            } else if (type == String.class) {
                int result = ((String) o1).compareTo((String) o2);

                if (result < 0) {
                    return -1;
                } else if (result > 0) {
                    return 1;
                } else {
                    return 0;
                }
            } else if (type == Boolean.class) {
                boolean b1 = ((Boolean) o1);
                boolean b2 = ((Boolean) o2);

                if (b1 == b2) {
                    return 0;
                } else if (b1) {
                    return 1;
                } else {
                    return -1;
                }
            } else {
                String s1 = o1.toString();
                String s2 = o2.toString();
                int result = s1.compareTo(s2);

                if (result < 0) {
                    return -1;
                } else if (result > 0) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }else{
            return 0;
        }
    }

  
    public int compare(int row1, int row2) {
        for (int level = 0; level < sortingColumns.size(); level++) {
            Integer column = sortingColumns.get(level);
            int result = compareRowsByColumn(row1, row2, column);
            if (result != 0) {
                return ascending ? result : -result;
            }
        }
        return 0;
    }

    
    public synchronized void sort() {
        if (table.getRowCount() > 0) {
            quickSort(0, (table.getRowCount() - 1));
        }
        System.out.println("Teste");
    }

    
    protected void quickSort(int first, int last) {
        if (first < last) {
            int low = first;
            int high = last;
            int mid = (high + low) / 2;

            while (low <= high) {
                while (compare(mid, low) > 0) {
                    low++;
                }
                while (compare(mid, high) < 0) {
                    high--;
                }
                if (low <= high) {
                    swap(low, high);
                    low = low + 1;
                    high = high - 1;
                }
            }
            
            if (first < high) {
                quickSort(first, high);
            }
            if (low < last) {
                quickSort(low, last);
            }
        }
    }

    abstract public void swap(int row1, int row2);

    public void sortByColumn(int column) {
        sortByColumn(column, true);
    }

    public void sortByColumn(int column, boolean ascending) {
        this.ascending = ascending;
        sortingColumns.removeAll(sortingColumns);
        sortingColumns.add(column);
        sort();
    }
}

Preciso setar os valores adquiridos pelo Quicksort, alguma ideia?

Criado 16 de setembro de 2016
Respostas 0
Participantes 1