Hi there Daniweb!
I have an assignment, but i am unfortunately stuck with one particular part of my assignment. Namely, as the title says, i need to sort an array of objects.
Basically I have a HairSalon class with properties, Name, Time, and Price
I then have a SortSalon Class which is where i am slightly stuck.
What i wish to do is sort my array of HairSalon objects by a property (such as the price, time, name)
I've looked on the internet and got terribly confused with comparators, as the instruction don't seem to be too friendly for beginners.
Here is my HairSalon class:
public class HairSalon
{
// instance variables - replace the example below with your own
private static int ProductTime;
private static double ProductPrice;
private static String Description;
public HairSalon(){
}
public HairSalon(double Price, String description, int Time)
{
ProductTime = Time;
Description = description;
ProductPrice = Price;
}
public double getPrice(){
return ProductPrice;
}
public int getTime(){
return ProductTime;
}
public String getName(){
return Description;
}
}
And here is my SalonSort class:
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class SortSalon extends HairSalon
{
public static void main(String[] args) {
HairSalon[] salonList = new HairSalon[6];
HairSalon a = new HairSalon(2.5, "Shower", 2);
HairSalon b = new HairSalon(5.5, "Coloring", 3);
HairSalon c = new HairSalon(10.5, "Haircut", 1);
HairSalon d = new HairSalon(5.5, "Pedicure", 4);
HairSalon e = new HairSalon(6.5, "Manicure", 5);
HairSalon f = new HairSalon(20, "Ultimate Package", 6);
salonList[0] = a;
salonList[1] = b;
salonList[2] = c;
salonList[3] = d;
salonList[4] = e;
salonList[5] = f;
sortName(salonList);
}
}
Just to recap, i want to sort my array of HairSalon objects (in this case salonList) by the name, price and time.
Thanks!