Java Comparator类对特定对象数组进行排序

alf*_*ibg 1 java android

我有一个float数组和一个String数组.每个浮点值与特定字符串匹配.我想使用以下方法对float数组进行排序以保留自己的字符串:

public static <T> void sort(T[] a,Comparator<? super T> c)
Run Code Online (Sandbox Code Playgroud)

这是代码:

public class ResultVoiceObject
{

     private  String frase;
     private float ranking;
     public ResultVoiceObject(String f, float r) 
       {
        this.frase=f;
        this.ranking= r;
       }  
     }
     public class VoiceRecognitionDemo extends Activity
     {

       // Populate the wordsList with the String values the recognition engine thought it heard
        ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);          
        //il Ranking
        float[] score= data.getFloatArrayExtra(RecognizerIntent.EXTRA_CONFIDENCE_SCORES);

        ResultVoiceObject[] risultati= new ResultVoiceObject[score.length];
        for (i=0; i<risultati.length;i++)
        {       
            risultati[i]=new ResultVoiceObject(matches.get(i), score[i]);       
        }          
        ResultVoiceObject[] risultatiDaOrdinare= risultati;  // risultati contais ResultVoiceObject elements
                    /*sorting*/
        }
Run Code Online (Sandbox Code Playgroud)

如何按排名和保留自己的字符串进行排序?

非常感谢.

gma*_*gma 7

ResultVoiceObject[] objects = ...
Arrays.sort(objects, new Comparator<ResultVoiceObject>() {

    @Override
    public int compare(ResultVoiceObject arg0, ResultVoiceObject arg1) {
        return Float.compare(arg0.getRanking(), arg1.getRanking());
    }

});
Run Code Online (Sandbox Code Playgroud)