Java Comparator class to sort particular object array

前端 未结 4 1584
日久生厌
日久生厌 2021-01-16 14:58

I have a float array and a String array. each float value match with a specific String. I would like to sort the float array keeping the own string using :



        
4条回答
  •  无人及你
    2021-01-16 15:32

    Assuming you have a getRanking() accessor for the private field ranking.

    public class ResultComparator implements Comparator {
      public int compare(ResultVoiceObject r1, ResultVoiceObject r2) {
        float f1 = r1.getRanking();
        float f2 = r2.getRanking();
        if(f1 > f2) return 1;
        else if(f1 < f2) return -1;
        return 0;
      }
    
    }
    
    Arrays.sort(resultsArray, new ResultComparator());
    

提交回复
热议问题