rendered paste bodyimport javax.swing.*;import java.awt.*;import java.awt.event.*;public class mainTable { JFrame f_main; Container c_main; JPanel p_main; JTable table; JScrollPane scrollPane; JButton sortAsc, sortDesc; JComboBox headerSelect; String tmp0, tmp1; Integer tmp2, tmp3; String[] columnNames = { "Last Name", "First Name", "Age", "Test Score" }; Object[][] data = { { "Smith", "Dave", new Integer(12), new Integer(75) }, { "Johnson", "Bill", new Integer(14), new Integer(72) }, { "Anderson", "Sara", new Integer(11), new Integer(92) }, { "Hoyez", "Kara", new Integer(16), new Integer(83) }, { "Cruz", "Kyle", new Integer(13), new Integer(84) } }; public mainTable() { f_main = new JFrame("CPSP.4 Table Sorting"); c_main = f_main.getContentPane(); p_main = new JPanel(); table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 80)); scrollPane = new JScrollPane(table); sortAsc = new JButton("Ascending"); sortDesc = new JButton("Descending"); sortAsc.addActionListener(new mainListener()); sortDesc.addActionListener(new mainListener()); headerSelect = new JComboBox(columnNames); headerSelect.addActionListener(new mainListener()); p_main.add(new JLabel("Sort by:")); p_main.add(headerSelect); p_main.add(sortAsc); p_main.add(sortDesc); p_main.add(scrollPane); c_main.add(p_main); f_main.setSize(520, 172); f_main.setVisible(true); } private class mainListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (event.getSource() == sortAsc) { switch (headerSelect.getSelectedIndex()) { // last name asc, selection case 0: for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { tmp0 = "" + data[i][0]; tmp1 = "" + data[i][1]; tmp2 = (Integer) data[i][2]; tmp3 = (Integer) data[i][3]; if (((String) data[i][0]) .compareTo((String) data[j][0]) > 0) { data[i][0] = data[j][0]; data[i][1] = data[j][1]; data[i][2] = data[j][2]; data[i][3] = data[j][3]; data[j][0] = tmp0; data[j][1] = tmp1; data[j][2] = tmp2; data[j][3] = tmp3; } } } break; // first name asc, insertion case 1: for (int i = 0; i < 5; i++) { tmp0 = "" + data[i][0]; tmp1 = "" + data[i][1]; tmp2 = (Integer) data[i][2]; tmp3 = (Integer) data[i][3]; } break; // age asc, selection case 2: for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { tmp0 = "" + data[i][0]; tmp1 = "" + data[i][1]; tmp2 = (Integer) data[i][2]; tmp3 = (Integer) data[i][3]; if (((Integer) data[i][2]) .compareTo((Integer) data[j][2]) > 0) { data[i][0] = data[j][0]; data[i][1] = data[j][1]; data[i][2] = data[j][2]; data[i][3] = data[j][3]; data[j][0] = tmp0; data[j][1] = tmp1; data[j][2] = tmp2; data[j][3] = tmp3; } } } break; // scores asc, insertion case 3: for (int i = 1; i < 4; i++) { int j = i - 1; tmp0 = "" + data[i][0]; tmp1 = "" + data[i][1]; tmp2 = (Integer) data[i][2]; tmp3 = (Integer) data[i][3]; while (j >= 0 && ((Integer) data[i][2]).compareTo((Integer) data[j][2]) < 0) { data[j+1][0] = data[j][0]; data[j+1][1] = data[j][1]; data[j+1][2] = data[j][2]; data[j+1][3] = data[j][3]; j--; } data[j+1][0] = tmp0; data[j+1][1] = tmp1; data[j+1][2] = tmp2; data[j+1][3] = tmp3; } break; } } if (event.getSource() == sortDesc) { switch (headerSelect.getSelectedIndex()) { // last name desc, selection case 0: for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { tmp0 = "" + data[i][0]; tmp1 = "" + data[i][1]; tmp2 = (Integer) data[i][2]; tmp3 = (Integer) data[i][3]; if (((String) data[i][0]) .compareTo((String) data[j][0]) < 0) { data[i][0] = data[j][0]; data[i][1] = data[j][1]; data[i][2] = data[j][2]; data[i][3] = data[j][3]; data[j][0] = tmp0; data[j][1] = tmp1; data[j][2] = tmp2; data[j][3] = tmp3; } } } break; // first name desc, insertion case 1: for (int i = 0; i < 5; i++) { tmp0 = "" + data[i][0]; tmp1 = "" + data[i][1]; tmp2 = (Integer) data[i][2]; tmp3 = (Integer) data[i][3]; } break; // age desc, selection case 2: for (int i = 0; i < 5; i++) { for (int j = i + 1; j < 5; j++) { tmp0 = "" + data[i][0]; tmp1 = "" + data[i][1]; tmp2 = (Integer) data[i][2]; tmp3 = (Integer) data[i][3]; if (((Integer) data[i][2]) .compareTo((Integer) data[j][2]) < 0) { data[i][0] = data[j][0]; data[i][1] = data[j][1]; data[i][2] = data[j][2]; data[i][3] = data[j][3]; data[j][0] = tmp0; data[j][1] = tmp1; data[j][2] = tmp2; data[j][3] = tmp3; } } } break; // scores desc, insertion case 3: for (int i = 0; i < 5; i++) { tmp0 = "" + data[i][0]; tmp1 = "" + data[i][1]; tmp2 = (Integer) data[i][2]; tmp3 = (Integer) data[i][3]; } break; } } f_main.update(f_main.getGraphics()); } }}