This is mycode sofar.
I already figure it out how to sort by name but i can't sort by price because compareTo don't accept int.
Any suggestion ?
Code:import java.util.*;
public class test
{
public static void main (String[] args)
{
Property[] Properties =
{
new Property(1234,"San Jose"),
new Property(9999,"Olive"),
new Property(5555,"Mountain View"),
new Property(4444,"Palo Altos"),
new Property(5333,"Foothill"),
};
Property.PrintArrayConsole("before:", Properties);
Property.ArraySort(Properties);
Property.PrintArrayConsole("After:", Properties);
}
}
class Property
{
private static boolean FloatLargestToTop(Property[] data, int top)
{
boolean changed = false;
Property temp;
// notice we stop at length -2 because of expr. k+1 in loop
for (int k =0; k < top; k++)
if (compareTo(data[k],"PRICE") > 0)
{
temp = data[k];
data[k] = data[k+1];
data[k+1] = temp;
changed = true;
}
return changed;
}
public static int compareTo(Property prep,String PRICE)
{
Property tmp = (Property)prep;
if(this.price < tmp.price)
{
/* instance lt received */
return -1;
}
else if(this.price > tmp.price)
{
/* instance gt received */
return 1;
}
/* instance == received */
return 0;
}
public static void ArraySort(Property[] array)
{
for (int k = 0; k < array.length; k++)
if ( !FloatLargestToTop(array, array.length-1-k) )
return;
}
// print out array with string as a title for the message box
public static void PrintArrayConsole(String title, Property[] data)
{
System.out.println("\n********** " + title + " **********\n");
for (int k =0; k < data.length; k++)
System.out.println( data[k].price+" " + data[k].address );
System.out.println();
}
// returns true if a modification was made to the array
private int price;
private String address;
public final static int RNT = 0;
public final static int CND = 1;
public final static int SFD = 2;
public final static int MAX_ROOMS = 20;
static final int MAX_PRICE = 10000000;
public final static int CONVERSION_FACTOR = 300;
public final static int MAX_ADDY_LENGTH = 500;
public final static int MAX_DAYS_ON_MARKET = 365 * 5; // 5 years
public final static String UNDEFINED = "[ undefined ]"; // for undefined strings
static final String PRICE = "ByPrice";
public String GetAddress()
{
return address;
}
public boolean SetAddress(String address)
{
if (address.length() > 0 && address.length() < MAX_ADDY_LENGTH)
{
this.address = address;
return true;
}
else
return false;
}
public boolean SetPrice(int price)
{
if ( price > 0 && price <= MAX_PRICE)
{
this.price = price;
return true;
}
else
return false;
}
public int GetPrice()
{
return price;
}
public Property()
{
price = 0;
address = UNDEFINED;
}
public Property(int price,String address)
{
SetPrice(price);
SetAddress(address);
}
}

