简体   繁体   English

无法对对象数组进行排序

[英]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. 问题如下:编写一个名为SortSalon的程序,该程序包含一个数组,用于容纳六个HairSalon对象,并用数据填充它。 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 使用Comparator对HairSalon对象进行排序,您可以定义自定义比较方式,并使用Arrays.sort(T[] a, Comparator<? super T> c)进行排序

You can either choose Comparable or Comparator for your sorting. 您可以选择ComparableComparator进行排序。 I prefer using Comparator , the reason is the comparison logic is in a separated class. 我更喜欢使用Comparator ,原因是比较逻辑在单独的类中。 That means you dont need to modify your HairSalon class, and you can have multiple self-defined Comparators class for different fields comparison/sorting. 这意味着您无需修改HairSalon类,并且可以具有多个自定义的Comparators类,用于不同的字段比较/排序。

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: 我认为最好的方法(使用已经定义的排序算法是使用接口“ Comparable”,例如:

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排序算法:

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. 但是,您在使用任何排序机制的特定实现时都遇到了麻烦,这无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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