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; int tmp2, tmp3; String[] columnNames = { "Last Name", "First Name", "Age", "Test Score" }; Object[][] data = { { "Smith", "Dave", new String("12"), new String("75") }, { "Johnson", "Bill", new String("14"), new String("72") }, { "Anderson", "Sara", new String("11"), new String("92") }, { "Hoyez", "Kara", new String("16"), new String("83") }, { "Cruz", "Kyle", new String("13"), new String("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++) { tmp0 = (String)data[i][0]; tmp1 = (String)data[i][1]; tmp2 = Integer.parseInt((String)data[i][2]); tmp3 = Integer.parseInt((String)data[i][3]); } break; // first name asc, insertion case 1: for (int i = 0; i < 5; i++) { tmp0 = (String)data[i][0]; tmp1 = (String)data[i][1]; tmp2 = Integer.parseInt((String)data[i][2]); tmp3 = Integer.parseInt((String)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]; //tmp0 is String, data[][] is Object, but [i][0] // is String... how to compare? // tmp0 = "" + data[i][0];? tmp1 = data[i][1]; tmp2 = data[i][2]; tmp3 = data[i][3]; if (data[i][2].compareTo(data[j][2]) > 0) { //and then how to compare? 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 = 0; i < 5; i++) { tmp0 = (String)data[i][0]; tmp1 = (String)data[i][1]; tmp2 = Integer.parseInt((String)data[i][2]); tmp3 = Integer.parseInt((String)data[i][3]); } break; } } if (event.getSource() == sortDesc) { switch (headerSelect.getSelectedIndex()) { // last name desc, selection case 0: for (int i = 0; i < 5; i++) { tmp0 = (String)data[i][0]; tmp1 = (String)data[i][1]; tmp2 = Integer.parseInt((String)data[i][2]); tmp3 = Integer.parseInt((String)data[i][3]); } break; // first name desc, insertion case 1: for (int i = 0; i < 5; i++) { tmp0 = (String)data[i][0]; tmp1 = (String)data[i][1]; tmp2 = Integer.parseInt((String)data[i][2]); tmp3 = Integer.parseInt((String)data[i][3]); } break; // age desc, selection case 2: for (int i = 0; i < 5; i++) { tmp0 = (String)data[i][0]; tmp1 = (String)data[i][1]; tmp2 = Integer.parseInt((String)data[i][2]); tmp3 = Integer.parseInt((String)data[i][3]); } break; // scores desc, insertion case 3: for (int i = 0; i < 5; i++) { tmp0 = (String)data[i][0]; tmp1 = (String)data[i][1]; tmp2 = Integer.parseInt((String)data[i][2]); tmp3 = Integer.parseInt((String)data[i][3]); } break; } } } }// public void insertionSort() {// int in, out;//// for (out = 1; out < 5; out++) // out is dividing line// {// templn = data[out][0]; // remove marked item// tempfn = data[out][1];// tempag = data[out][2];// tempsc = data[out][3];// in = out; // start shifts at out// while (in > 0 && a[in - 1] >= temp) // until one is smaller,// {// a[in] = a[in - 1]; // shift item right,// --in; // go left one position// }// a[in] = temp; // insert marked item// } // end for// } // end insertionSort()//// public static void selectionSort1(int[] x) {// for (int i = 0; i < x.length - 1; i++) {// for (int j = i + 1; j < x.length; j++) {// if (x[i] > x[j]) {// // ... Exchange elements// int temp = x[i];// x[i] = x[j];// x[j] = temp;// }// }// }// }}