I am trying to sort an array of objects by field, but I keep getting this error saying that "price / location cannot be resolved or is not a field", even though they both are fields, declared in the class Realestate. I got the same error about "not being a field" when I was dealing with ArrayList as well, so I moved to simple arrays of objects and I'm still getting the same error. What am I doing wrong? I found info about comparing arrays with Comparable and Comparator but I don't undertand how to apply whatever results to the sorting routine. Please HELP! These are my files:
File Realestate:
public class Realestate {
static String location = "";
static String description = "";
static String price = "";
// constructor
public Realestate(String loc, String desc, String prc) {
location = loc;
description = desc;
price = prc;
}
}
File RealestateFinder:
import java.util.*;
public class RealestateFinder {
// Hold properties' info
static String[] locations = {"Miami", "Las Vegas", "Paris", "London"};
static String[] descriptions = {"1-bedroom", "2-bedrooms", "3-bedrooms", "penthouse"};
static String[] prices = {"1,000,000", "2,000,000", "3,000,000", "4,000,000"};
// Sort per field
public static void sortArray(Realestate arr[]) {
for(int x = 0; x < arr.length; ++x) {
//use array Sort
Arrays.sort(arr);
}
}
public static void main(String[] args) {
int numProperties = 4;
//Create new Realestate object newProperty
Realestate[] newProperty = new Realestate[numProperties];
// Initialize array of objects Realestate[] newProperty using arrays from top
for(int x = 0; x < newProperty.length; ++x) {
newProperty[x] = new Realestate(locations[x], descriptions[x], prices[x]); // constructor
}
//Get user's input
//...
while(true) {
if(holdUserInput == 1) {
//sort per price
sortArray(newProperty.price);//price cannot be resolved or is not a field
break;
}
else if(holdUserInput == 2) {
// sort per location
sortArray(newProperty.location);//location cannot be resolved or is not a field
break;
}
else {
System.out.println("Program Ended \n");
break;
}// end else statement
}// end while loop
}// end main()
}// end class RealestateFinder
newPropertyis an array - the array doesn't have a price... Sounds like you might want to read stackoverflow.com/questions/5932720