Using selection sort in java to sort floats

Posted by user334046 on Stack Overflow See other posts from Stack Overflow or by user334046
Published on 2010-05-06T03:10:15Z Indexed on 2010/05/06 3:18 UTC
Read the original article Hit count: 468

Filed under:
|
|

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:

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about sort