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

时间:2013-06-18 08:31:48

标签: java android

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

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 个答案:

答案 0 :(得分:7)

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

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

});

答案 1 :(得分:3)

假设您拥有私人字段getRanking()的{​​{1}}访问者。

ranking

答案 2 :(得分: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

答案 3 :(得分:0)

我解决了合并两个answare的问题:

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;
   }  
 }

另一种方式是:

ADD

import java.util.Arrays;

import java.util.Comparator;

REMOVE:实现Comparator和比较方法onActivityResult

感谢stackoverflow社区!