I didn't feel like rewriting code. So i combined 4 sorts in one. This will sort an arraylist from highest to lowest, accessing the elements double values or the string values.
So basically i can sort the items in my grocerystore by price(lowest to highest, or highest to lowest), or alphabetically (A-Z or Z-A).
I am still working out some kinks to make it a bit more effeceint. I don't plan to change the method i am using to sort it though even though i am certain there are better ways.
Here is the code!
public ArrayList<Food> sort(ArrayList<Food> list, boolean high,boolean price)
{
int scope = list.size();
for(int i = 0;i<scope;i++)
{
Food temp = list.get(i);
int size = sortedList.size();
boolean added = false;
if(size > 0)
{
double price = temp.getPrice();
String name = temp.getName();
outerloop:
for(int j=0; j<size ;j++)
{
Food foodTemp = sortedList.get(j);
double tempPrice = foodTemp.getPrice();
String tempName = foodTemp.getName();
if(high == true)
{
if(price == true)
{
if( price > tempPrice)
{
sortedList.add(j, temp);
added = true;
break outerloop;
}
}else{
if( name > tempName)
{
sortedList.add(j, temp);
added = true;
break outerloop;
}
}
} else {
if(price == true)
{
if( price < tempPrice)
{
sortedList.add(j, temp);
added = true;
break outerloop;
}
}else{
if( name < tempName)
{
sortedList.add(j, temp);
added = true;
break outerloop;
}
}
}
}
if(added == false)
{
sortedList.add(temp);
}
}else{
sortedList.add(temp);
}
}
list.clear();
return sortedList;
}
Basically 4 functions jammed into one lol.