Java Comparator class to sort particular object array

前端 未结 4 1579
日久生厌
日久生厌 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<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());
    
    0 讨论(0)
  • 2021-01-16 15:43
    ResultVoiceObject[] objects = ...
    Arrays.sort(objects, new Comparator<ResultVoiceObject>() {
    
        @Override
        public int compare(ResultVoiceObject arg0, ResultVoiceObject arg1) {
            return Float.compare(arg0.getRanking(), arg1.getRanking());
        }
    
    });
    
    0 讨论(0)
  • 2021-01-16 15:53

    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!

    0 讨论(0)
  • 2021-01-16 15:54

    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

    0 讨论(0)
提交回复
热议问题