1

So I have all this data that I'm eventually feeding into an object that is to be displayed into a JTable. What I need to do is sort all of the data by the Averages and feed them into the Object for the JTable in a sorted fashion that shows the data from High to low by average.

This is all of my data

private String[] Names = new String[10];
private char[] Sex = new char[10];
private int[][] Score = new int[10][5];
private double[] Averages = new double[10];
private char[] LetterGrades = new char[10];

This is how I feed it in unsorted

Object[][] data = new Object[10][9];
   for(int i = 0; i < 10; i++){
      data[i][0] = Names[i];
      data[i][1] = Sex[i];
      data[i][2] = Score[i][0];
      data[i][3] = Score[i][1];
      data[i][4] = Score[i][2];
      data[i][5] = Score[i][3];
      data[i][6] = Score[i][4]
      data[i][7] = Averages[i];
      data[i][8] = LetterGrades[i];
    }

I've been thinking of doing it with an ArrayList but I can't seem to wrap my head around how to feed it into an array list than sort it by the Average column. I'm not really sure if I can sort an object. Any insight or a point in the right direction would be awesome.

4

1 回答 1

4

JTable has sorting capabilities built in.

You can activate it by calling table.setAutoCreateRowSorter(true);

See Sorting and Filtering for more details

于 2013-09-24T23:45:56.670 回答