我有一个充满对象的数组列表。每个对象由两个整数组成。如何根据第一个整数对数组列表进行排序,同时将第二个整数与其原始整数保持一致?然后如何添加排序数组列表的所有第二个整数?
我试过这个:
Collections.sort(info, new Comparator()
{
public int compare(M one, M two)
{
return m1.getCost().compareToIgnoreCase(m2.getCost());
}
});
class M{
//Declares the attributes belonging to this class
private int money;
private int cost;
//Constructor method
{
//this refers to object in which the method was called
this.cost = cost;
this.money = money;
}
//Returns the cost variable
public int getCost()
{
return cost;
}
public void setCost(int cost)
{
this.cost = cost;
}
//Returns the maximum amount
public int getMoney()
{
return money;
}
public void setMoney(int Money)
{
this.Money = Money;
}
}
我是java新手,所以任何帮助将不胜感激(: