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!

I don't think you grasp the inheritance principle very well.
your SortSalon should not extend HairSalon. not only aren't you using it, it makes no sense to do so.

just have your HairSalon class implement the Comparable interface, and create the implementation of compareTo as you see fit. once that's done, you can write any which sorting algorithm, or you can use the pre-defined sorting functionality: Arrays.sort

My assignment says to do it in a seperate class, so that's why I am doing it.

As to using the comparable interface, do you have any specifics as to how to do that?

Obviously i've googled it before but the answers were a bit confusing, so i'm here for some clarification.

Sriramtashua: there's nothing wrong with doing it in a separate file, nor is there anything wrong with that code, but there's no reason why that class should extend HairSalon.

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.