First and foremost, I've read a bunch of links on the web and I still can't implement the methods I've found in this specific context, please help me by providing me some sort of guidance.

The object in question is the Property Object.

It has 3 attributes

int value, int rent, Person owner (assuming class Person exists).

I need to create an array of 10 Property objects, assign the value attribute with random numbers and sort them by value in ascending order:

Property[] street = new Property [10];
     
     for (int i=0;i<street.length;i++)    
     {
     int propertyValue = (int)(200000*Math.random());
     int propertyRent =  (int)(49*Math.random())+1;
     
     street[i] = new Property(propertyValue, propertyRent, null);
     System.out.println (street[i]);
      
      
     }

Basically what I need to do is this:

If I have 3 Property objects with the following attributes:

property value: $181,230.00 rent: $22.00 owner: null
property value: $112,169.00 rent: $15.00 owner: null
property value: $128,237.00 rent: $43.00 owner: null

I need to sort them in ascending order by value and display them like this:

property value: $112,169.00 rent: $15.00 owner: null
property value: $128,237.00 rent: $43.00 owner: null
property value: $181,230.00 rent: $22.00 owner: null

Hello!

Have you heard of the interface Comparable?

It allows you to compare like-objects with its implementation.

public static void sort(Comparable[] values, int start, int end)

If your class implemented Comparable you would be forced to include a method called compareTo(Object o)

The reason this is nice is because the compiler recognizes anything that implements the interface as a Comparable object, which means you can use an array of any objects you want for the argument of the method above so long as they implement Comparable.

You will need to override this method since it does nothing on its own. You can make some attributes for it, like so--

//Method to be included in your Property class

public int compareTo(Object arg) //comparing to another object (a different Property)
{
      if(arg instanceof Property) //if the object is-a property object
      {
             Property temp = (Property)arg; //Edit

              if(temp.propertyValue == propertyValue)
                  return 0;
              else if(temp.propertyValue < propertyValue)
                  return 1;
              else return -1;
      }
     return 99;
}

Now you can sort different property objects using the compareTo method. If you compare two Property objects and the value (from the method) returned is -1, you know that the class that used the compareTo method has a lesser property and should be placed before the class it was comparing to.

On the other hand, if the value is 1 then you know that the class that used the compareTo method has a greater value and should come after the class it was comparing to.

If the result is zero then both classes have equal Property value and whichever ones comes before the other doesn't really matter in terms of sorting.

From there, all you have to do is making a condition to how the objects will sorted. Use a for loop to iterate through the objects and use the compareTo method that each object should have (since they implement Comparable).

Edit: Had to place a necessary cast from Object down to Property. See the edited code above.

commented: Good post +9

Alex I think you might have saved my life, I will get right on it and try to implement what you just wrote!

Thanks! I will get back to you with results

Thanks a lot that code worked then I just used the Arrays.sort method!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.