package com.opensource.sort;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.collections.CollectionUtils;
/**
*
* @author Cache John
* @email 550595698@qq.com
*
*/
public class ComparatorSort
{
private static final List<AogInfo> avaibleAogList = new CopyOnWriteArrayList<AogInfo>();
/**
* 获取最小负载数
*
* @param avaibleAogList
* @return
*/
private static AogInfo getMinLoadAogInfo(final List<AogInfo> avaibleAogList)
{
AogInfo aogInfo = null;
if (CollectionUtils.isNotEmpty(avaibleAogList))
{
AogInfo[] arrays = new AogInfo[avaibleAogList.size()];
avaibleAogList.toArray(arrays);
Arrays.sort(arrays, new Comparator<AogInfo>()
{
@Override
public int compare(AogInfo o1, AogInfo o2)
{
return (int)(o1.getAssignNo() - o2.getAssignNo());
}
});
aogInfo = arrays[0];
}
return aogInfo;
}
/**
* @param args
*/
public static void main(String[] args)
{
// 生成随即对象
Random random = new Random();
for (int i = 0; i < 5; i++)
{
avaibleAogList.add(new AogInfo(random.nextInt(100)));
}
// 生成随即对象
System.out.println("生成随即对象为:");
for (AogInfo o : avaibleAogList)
{
System.out.println(o.getAssignNo());
}
// 获取最小随即对象
System.out.println("获取最小随即对象为:" + getMinLoadAogInfo(avaibleAogList).toString());
}
}
class AogInfo
{
private int assignNo;
public AogInfo(int assignNo)
{
this.assignNo = assignNo;
}
public int getAssignNo()
{
return assignNo;
}
public void setAssignNo(int assignNo)
{
this.assignNo = assignNo;
}
@Override
public String toString()
{
return "AogInfo [assignNo=" + assignNo + "]";
}
}