JAVA实现比较器

package com.opensource.comparator;

public class Pojo
{
    private int value;
   
    public Pojo(int value)
    {
        this.value = value;
    }
   
    public int getValue()
    {
        return value;
    }
   
    public void setValue(int value)
    {
        this.value = value;
    }
   
    @Override
    public String toString()
    {
        return "Pojo [value=" + value + "]";
    }
   
}

 

 

 

 

package com.opensource.comparator;

import java.util.Comparator;

public class PojoComparator implements Comparator<Pojo>
{
   
    @Override
    public int compare(Pojo o1, Pojo o2)
    {
        return o1.getValue() - o2.getValue();
    }
   
}

 

 

 

 

package com.opensource.comparator;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Main
{
    private final static List<Pojo> avaibleList = new CopyOnWriteArrayList<Pojo>();
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        avaibleList.add(new Pojo(2));
        avaibleList.add(new Pojo(1));
        avaibleList.add(new Pojo(3));
       
        Pojo[] arrays = new Pojo[3];
        avaibleList.toArray(arrays);
        Arrays.sort(arrays, new PojoComparator());
       
        System.out.println(arrays[0].toString());   
    }
   
}

 

你可能感兴趣的:(JAVA实现比较器)