Java Comparator class to sort particular object array

前端 未结 4 1638
日久生厌
日久生厌 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: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 {
       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 {
    
    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 list = new ArrayList();
        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

提交回复
热议问题