1

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 :

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

Here is the code:

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

How can I sort by ranking and keeping the own String?

Thanks a lot.

7
  • please show us the code you tried to solve this problem with Commented Jun 18, 2013 at 8:34
  • I don't have code, I don't know how to use Arrays.sort with object like mine. I solved in another way but my manager would likes I use Array.sort(result, Comparator). Can I? Commented Jun 18, 2013 at 8:36
  • 1
    yes you can and google will help Commented Jun 18, 2013 at 8:37
  • you need to create your own comparator, for example ResultVoiceObjectComparator Commented Jun 18, 2013 at 8:37
  • Shouldn't that be phrase? : S Commented Jun 18, 2013 at 8:40

4 Answers 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());
    }

});
Sign up to request clarification or add additional context in comments.

4 Comments

error: Multiple markers at this line - Comparator cannot be resolved to a type - The method sort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (ResultVoiceObject[], new Comparator<ResultVoiceObject>(){})
import java.util.Arrays; import java.util.Comparator;
yes it's true! So I deleted the implements and compare methos! I voted you thanks!
I posted a complete solution!
3

Assuming you have a getRanking() accessor for the private field 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());

4 Comments

Watch out! Implementing comparator by using arithmetic operations can result in wrong sort order because of overflow.
(int)r1.getRanking() - (int)r2.getRanking(); meaning that i.e. 7.1 == 7.2?
@gma, I was in the process of editing my post. Sorry for that.
thanks i solved with another solution, you can see on my post
3

You need to implement the Comparator interface. You can create multiple sort sequences while using Comparator.

Suppose you want to sort your array according to your ranking, then create a separate class which implements Comparator

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

Then in the new class that you want to sort your array, You create the object of the comparator and pass it to collection

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

This is the overloaded version of sort method which takes the comparator.

I had written a full tutorial regarding this a while ago http://www.dreamincode.net/forums/topic/174322-the-comparable-and-comparator-interface-part-ii/

Here is the ResultVoicObject class

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
            + "]";
}

}

Implement the Comparator interface as follows, you need to implement compare method

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

You can test it as below.

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

}
  }

If you want to create a sorting sequence using frase, then you just need to create a new comparator class for it and sort just as I have sorted above

Hope this helps... took a lot of efforts from me also :D :D

Comments

0

I solved merging both 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;
}

}

Where:

 public class ResultVoiceObject
 {

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

Another way is:

ADD

import java.util.Arrays;

import java.util.Comparator;

REMOVE: implements Comparator and the compare methos out onActivityResult

Thanks stackoverflow comunity!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.