Hey, I need to sort an array list of class house by a float field in a certain range. This is my code for the selection sort: Basically sortPrice copies the stuff in this list into a new one selecting only the values in that range, then it does a selection sort in that array list. The arraylist (x) in sortPrice is unsorted an needs to be copied because what x references cannot be altered.
public ArrayList<House> sortPrice(ArrayList<House> x,float y, float z){
ArrayList<House> xcopy = new ArrayList<House>();
for(int i = 0; i<x.size(); i++){
if(x.get(i).myPriceIsLessThanOrEqualTo(z) && x.get(i).myPriceIsGreaterThanOrEqualTo(y)){
xcopy.add(x.get(i));
}
}
ArrayList<House> price= new ArrayList<House>();
while(xcopy.size()>0){
House min = xcopy.get(0);
for(int i = 1; i < xcopy.size();i++){
House current = xcopy.get(i);
if (current.myPriceIsGreaterThanOrEqualTo(min.getPrice())){
min = current;
}
}
price.add(min);
xcopy.remove(min);
}
return price;
}
Here is what the house class looks like:
public class House {
private int numBedRs;
private int sqft;
private float numBathRs;
private float price;
private static int idNumOfMostRecentHouse = 0;
private int id;
public House(int bed,int ft, float bath, float price){
sqft = ft;
numBathRs = bath;
numBedRs = bed;
this.price = price;
idNumOfMostRecentHouse++;
id = idNumOfMostRecentHouse;
}
public boolean myPriceIsLessThanOrEqualTo(float y){
if(Math.abs(price - y)<0.000001){
return true;
}
return false;
}
public boolean myPriceIsGreaterThanOrEqualTo(float b){
if(Math.abs(b-price)>0.0000001){
return true;
}
return false;
}
When i call looking for houses in range 260000.50 to 300000 I only get houses that are at the top of the range even though I have a lower value at 270000. Can someone help?