1

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

public static <T> void sort(T[] a,Comparator<? super T> c)

这是代码:

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*/
        }

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

非常感谢。

4

4 回答 4

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

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

});
于 2013-06-18T08:41:09.773 回答
3

假设您有getRanking()私有字段的访问器ranking

public class ResultComparator implements Comparator<ResultVoiceObject> {
  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());
于 2013-06-18T08:37:43.667 回答
3

您需要实现Comparator接口。您可以在使用Comparator.

假设您想根据排名对数组进行排序,然后创建一个单独的类来实现Comparator

public Class RankingSorter implements Comparator<ResultVoiceObject> {
   public int compare(ResultVoiceObject one, ResultVoiceObject another){
       return (int)(one.getRanking() - another.getRanking());
   }
}

然后在要对数组进行排序的新类中,创建对象comparator并将其传递给集合

RankingSorter rs = new RankingSorter();
Collections.sort(yourArray, rs);

这是sort方法的重载版本,它采用comparator.

不久前我写了一个完整的教程 http://www.dreamincode.net/forums/topic/174322-the-comparable-and-comparator-interface-part-ii/

这是 ResultVoicObject 类

package com.compare;

public class ResultVoiceObject {

private String frase;
private float ranking;

public ResultVoiceObject(String f, float r) {
    this.frase = f;
    this.ranking = r;
}

public String getFrase() {
    return frase;
}

public void setFrase(String frase) {
    this.frase = frase;
}

public float getRanking() {
    return ranking;
}

public void setRanking(float ranking) {
    this.ranking = ranking;
}

@Override
public String toString() {
    return "ResultVoiceObject [frase=" + frase + ", ranking=" + ranking
            + "]";
}

}

实现Comparator接口如下,需要实现compare方法

 package com.compare;

 import java.util.Comparator;

  public class RankingSort implements Comparator<ResultVoiceObject> {

public int compare(ResultVoiceObject one, ResultVoiceObject another){
    return (int) (one.getRanking() - another.getRanking());
}
  }

您可以按如下方式对其进行测试。

 package com.compare;

 import java.util.ArrayList;
 import java.util.Collections;

 public class RankingSorterTest{

public static void main(String [] args){

    ArrayList<ResultVoiceObject> list = new ArrayList<ResultVoiceObject>();
    list.add(new ResultVoiceObject("one", 1));
    list.add(new ResultVoiceObject("five", 5));
    list.add(new ResultVoiceObject("three", 3));

    Collections.sort(list,new RankingSort());
    System.out.println(list);

}
  }

如果你想使用 frase 创建一个排序序列,那么你只需要comparator为它创建一个新类并按照我上面排序的方式进行排序

希望这会有所帮助......我也付出了很多努力:D:D

于 2013-06-18T08:43:22.920 回答
0

我解决了合并两个答案:

public class VoiceRecognitionDemo extends Activity implements Comparator<ResultVoiceObject {

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
        ...
        ResultVoiceObject[] risultati= ...;     
        Arrays.sort(risultati, new Comparator<ResultVoiceObject>() {
            @Override
            public int compare(ResultVoiceObject arg0, ResultVoiceObject arg1) {
                return Float.compare(arg0.getRanking(), arg1.getRanking());
            }
        });  

    }


public int compare(ResultVoiceObject lhs, ResultVoiceObject rhs) {
    // TODO Auto-generated method stub
    return 0;
}

}

在哪里:

 public class ResultVoiceObject
 {

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

另一种方法是:

添加

导入 java.util.Arrays;

导入 java.util.Comparator;

REMOVE:实现比较器和比较方法出 onActivityResult

感谢stackoverflow社区!

于 2013-06-18T09:04:29.833 回答