blaces
blaces

Reputation: 495

Sort an Array and get the 3 largest element

I am beginner java programmer. I have this homework: Write a static method, in the parameter of method is an Drink array. The method returns with the 3 largest alcohol drink array element.

My question is: Is there a shorter solution as I wrote?

    public static void alcoholMax(AlcoholDrink[] t){
    // sort the AlcoholDrink[] t
    Arrays.sort(t, new Comparator<AlcoholDrink>() { 
        // here I try sorting the elements by their alcohol value
        @Override
        public int compare(AlcoholDrink o1, AlcoholDrink o2) {
            int at1 = (int)o1.getAlcohol(); // get the alcohol value
            int at2 = (int)o2.getAlcohol(); // get the alcohol value
            if(at1>at2)
                return -1;
            else if(at1<at2 )
                return 1;
            return 0;
        }
    });
    // get the 3 largest element of the sorted array
    double t0 = t[0].getAlcohol();
    double t1 = t[1].getAlcohol();
    double t2 = t[2].getAlcohol();
    // check, 1st > 2nd > 3rd, if this not true, return with null reference
    if(t0>t1 && t1>t2)
        System.out.println(t[0] + "\n" + t[1] + "\n" + t[2]);
    else
        System.out.println("null");
}

public static void main(String[] args){
    AlcoholDrink[] t = new AlcoholDrink [8];
    // new AlcoholDrink( String Name, String Stripping, int price, double alcohol)
    // these are hungarian wines :). If somebody curious for the languange
    t[0] = new AlcoholDrink("Kék Portói", "0.75 l", 1200, 20.5);
    t[1] = new AlcoholDrink("Kék Oportó", "0.75 l", 1100, 10.5);
    t[2] = new AlcoholDrink("Tokaji Asszú", "0.75 l ", 1600, 14.5);
    t[3] = new AlcoholDrink("Egri Bikavér", "0.75 l", 1500, 23.5);      
    t[4] = new AlcoholDrink("Egri Leányka", "0.75 l", 1100, 8.5);
    t[5] = new AlcoholDrink("Egri Merlot", "0.75 l", 1700, 18.5);
    t[6] = new AlcoholDrink("Egri Medina", "0.75 l", 900, 16.5);
    t[7] = new AlcoholDrink("Törley Talisman", "0.75 l", 750, 4.5);     

    alcoholMax(t);
    // It is always return with "null"

Upvotes: 0

Views: 497

Answers (4)

ravi
ravi

Reputation: 1

First sort the array descending order and get the first three element.

package sortingelementinarray;
public class SortElement 
{
    public static void main(String args[])
    {
        int array[] = {1,6,4,7,2,3};
        int temp;
        for(int j = 0 ; j < array.length; j++)
        {
        for(int i = 0; i < array.length-1; i++)
        {
            if(array[j] > array[i])
            {
                temp = array[j];
                array[j] = array[i];
                array[i] = temp;

            }
        }
        }
        for(int abc : array)
        {
            System.out.println(abc);
        }

    }
}

Upvotes: 0

npinti
npinti

Reputation: 52185

If your getAlcohol() method returns a double, then, you should not cast it to an int, this will cause a loss of precision. Also, you can compare doubles automatically instead of doing it yourself, like so:

Arrays.sort(t, new Comparator<AlcoholDrink>() { 
        // here I try sorting the elements by their alcohol value
        @Override
        public int compare(AlcoholDrink o1, AlcoholDrink o2) {
           return o1.getAlcohol().compareTo(o2.getAlcohol());
        }
    });

You can also make your Alcohol class implement the Comparable Interface as shown in this example.

Lastly, if you want to stick to your own code, you might want to consider making a change to the value returned by the compare method like so:

 @Override
        public int compare(AlcoholDrink o1, AlcoholDrink o2) {
            int at1 = (int)o1.getAlcohol(); // get the alcohol value
            int at2 = (int)o2.getAlcohol(); // get the alcohol value
            if(at1>at2)
                return -1;
            else if(at1<at2 )
                return 1;
            return 0;
        }

I can't test the code at the moment, but you might be sorting the array in an ascending fashion.

Upvotes: 3

r0ast3d
r0ast3d

Reputation: 2635

in your comparator, return -1 if it is less than, 1 if it is greater than and that should work

Upvotes: 0

pcalcao
pcalcao

Reputation: 15990

Your array is being sorted in ascending order I believe.

In that case, you want to get the last 3 elements after the sort, or change your comparator.

Upvotes: 0

Related Questions