对包含自定义对象的ArrayList进行排序

时间:2014-11-21 08:28:58

标签: sorting recursion arraylist

我想对高分数的ArrayList进行排序。该列表包含对象,每个对象包含String name,String category和int score。我想按分数将ArrayList按降序排序:

这就是我现在所拥有的,但是当程序运行时它会崩溃程序:

**INITIALIZING ARRAY AND VARIABLES:**
private static ArrayList<HighScore> highScores = new ArrayList<HighScore>();
private static int Last;
private static int First = 0;
private static int PivotValue;
private static  int LeftPointer;
private static int RightPointer;
private static int Pivot;


  **READING IN VALUES INTO ARRAYLIST**
 String File = "src/project/res/highscores.txt";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(File));
            while ((line = br.readLine()) != null) {
                HighScore h = new HighScore(line);
                highScores.add(h);
            }
        } catch (Exception e) {
            System.out.println("Problem reading in high scores!");
        }


**CALLING THE PROCEDURE**
    quickSort(highScores,First,Last);




private static void quickSort(ArrayList<HighScore> highScores2, int first, int last){

        if(first < last)
        {
            PivotValue = highScores2.get(first).getScore();
            LeftPointer = first + 1;
            RightPointer = last;

            while((LeftPointer <= RightPointer))
            {
                while((LeftPointer <= RightPointer)&&(highScores2.get(LeftPointer).getScore() < PivotValue))
                {
                    LeftPointer++; 
                }
                while((highScores2.get(RightPointer).getScore() > PivotValue)&&(LeftPointer <= RightPointer))
                {
                    RightPointer--;
                }
                if(LeftPointer < RightPointer)
                {
                    HighScore temp = highScores2.get(LeftPointer);
                    highScores2.set(RightPointer, highScores2.get(LeftPointer));
                    temp.equals(highScores2.get(RightPointer));
                }
            }

            Pivot = RightPointer;
            HighScore temp = highScores2.get(first);
            highScores2.set(Pivot,highScores2.get(first));
            temp.equals(highScores2.get(Pivot));

            quickSort(highScores2, first, Pivot-1);
            quickSort(highScores2, Pivot + 1, last);
        }


    }

0 个答案:

没有答案