简体   繁体   中英

Trouble sorting an array of objects

Question is as follows: Write a program named SortSalon that contains an array to hold six HairSalon objects and fill it with data. Include a method to sort the array in ascending order by price of service. Call the method and display the results.

public class SortSalon
{
    private static HairSalon [] itsArray = new HairSalon[6];

    public SortSalon()
    {
        itsArray[0] = new HairSalon("cut", 10.50, 15);
        itsArray[1] = new HairSalon("shampoo", 5.00, 10);
        itsArray[2] = new HairSalon("manicure", 20.00, 20);
        itsArray[3] = new HairSalon("cut", 10.50, 15);
        itsArray[4] = new HairSalon("manicure", 20.00, 20);
        itsArray[5] = new HairSalon("manicure", 20.00, 20);
    }

    public HairSalon [] sortByPrice(HairSalon [] par)
    {
        HairSalon [] newArray = new HairSalon[6];
        int x = 0;
        int y = 0;
        HairSalon smallest = itsArray[1];

        for(int i = 0; i < itsArray.length; i++)
        {
            while(y < itsArray.length - 1)
            {
                if(itsArray[y].getPrice() == smallest.getPrice())
                {
                    smallest = itsArray[y];
                    newArray[x] = smallest;
                }
                else
                {
                    //smallest = itsArray[y];
                    for(int c = 0; c < itsArray.length - 1; c++)
                    {
                        if(itsArray[y].getPrice() < itsArray[y + 1].getPrice() 
                        && itsArray[y].getPrice() >  smallest.getPrice())
                        {
                            smallest = itsArray[y];
                            newArray[x] = smallest;
                        }
                    }
                }
                y++;
            }
            y = 0;
            //newArray[x] = smallest;
            x++;
        }

        int z = 0;
        System.out.println("Ascending order: ");

        while(z < newArray.length)
        {
            System.out.println(newArray[z].toString());
            z++;
        }
        return newArray;
    }

    public static void main()
    {
        SortSalon test = new SortSalon();
        test.sortByPrice(itsArray);
    }
}

Can't get the method to correctly sort the objects by price. Any suggestions will be appreciated!

Sort your HairSalon objects with Comparator , you can define your custom comparison way, and using Arrays.sort(T[] a, Comparator<? super T> c) for your sorting

You can either choose Comparable or Comparator for your sorting. I prefer using Comparator , the reason is the comparison logic is in a separated class. That means you dont need to modify your HairSalon class, and you can have multiple self-defined Comparators class for different fields comparison/sorting.

Sample:

public class HairSalon {
    private final String type;
    private final double price;

    public HairSalon(String type, double price){
        this.type = type;
        this.price = price;
    }

    public String getType(){ return this.type;}
    public double getPrice(){return this.price;}
}


public class PriceComparator implements Comparator<HairSalon> {

/**
 *comparison/sorting logic is here
 */
@Override
public int compare(HairSalon hs1, HairSalon hs2)
{
    if(hs1.getPrice() > hs2.getPrice()){return 1;}
    else if(hs1.getPrice() == hs2.getPrice()){return 0;}
    else{return -1;}
}

}

How you sort them:

    //if you put all your HairSalon objects in an array:
    Arrays.sort(hairSalonsArray, new PriceComparator())    

    //if you put all your HairSalon in a list
    Collections.sort(hairSalonsList, new PriceComparator());

I think the best way (using already defined sorting algoritms is using the interface "Comparable", for Example:

private class HairSalon implements Comparable<HairSalon>{
    public String type = null;
    public double price = 0;
    public float number = 0;

    public HairSalon(String type,double price,float number){
        this.type = type;
        this.price = price;
        this.number = number;
    }

    @Override
    public int compareTo(HairSalon compa) {
        int ret = 0;
        if(this.price < compa.price) ret = -1;

        if(this.price > compa.price){
            ret = 1;
        }else{ret = 0;}

        return ret;
    }

}

Then you can use the Collections sorting algorithms:

Collections.sort(new ArrayList<HairSalon>(Arrays.asList(itsArray)));

But iof you are having trouble with an specific implementation of any sorting mechanism this wouldn't help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM