I have an arraylists filled with objects. Each object consists of two integers. How do i sort the arraylist based on the first integer while keeping the second integer with its original integer? And then how to I add all of the second integers of the sorted arraylist?
I tried this:
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;
}
}
I am new to java so any help would be greatly appreciated(: