简体   繁体   中英

Sorting an arraylist of arraylists of objects using double type

I have an arraylist of arraylists, each arraylist within the outer arraylist holds the following values holidayId, country, countryValue, duration, durationValue, accType, accTypeValue, holSubType, holSubTypeValue, weather, weatherValue, pricePP, pricePPValue, recommendationScore. These are all held as objects, I want to know if there is a way to sort the arraylists using the recommendationScore by maybe converting it into a double type and sorting it that way? Or any other way I could approach this problem?

Thanks in advance

public class RecAlgorithmImpl {

public static ArrayList<Double> attRanking(ArrayList<Double> attRank){

    ArrayList<Double> attWeight = new ArrayList<>();
    double weight;

    /*Loops through the rankings given in the questionnaire 
     * and sets a new number for better recommendation accuracy */
    for(int i = 0; i < attRank.size();i++){

        if(attRank.get(i) == 1){
            attRank.set(i, 7.0);
        }
        else if(attRank.get(i) == 2){
            attRank.set(i, 6.0);
        }
        else if(attRank.get(i) == 3){
            attRank.set(i, 5.0);
        }
        else if(attRank.get(i) == 4){
            attRank.set(i, 4.0);
        }
        else if(attRank.get(i) == 5){
            attRank.set(i, 3.0);
        }
        else if(attRank.get(i) == 6){
            attRank.set(i, 2.0);
        }           
        else if(attRank.get(i) == 0){
            attRank.set(i, 1.0);
        }
    }

            //Loop through the ranked values and assign a weighting to each attribute
    for(int j = 0; j < attRank.size(); j++){
        weight = (attRank.get(j))/28;
        attWeight.add(weight);
    }

    for(int k = 0; k < attWeight.size();k++){
        System.out.println(attWeight.get(k));
    }
    return attWeight;
}

public static ArrayList<ArrayList> valuePoints(ArrayList<ArrayList> valuePoints){
        //DataRetrievalImpl database = new DataRetrievalImpl();
        ArrayList<ArrayList> holiday = new ArrayList<>();

        //test info
        ArrayList<String> test = new ArrayList<>();
        test.add("stuff1");
        test.add("stuff2");
        test.add("stuff3");
        test.add("stuff4");
        test.add("stuff5");
        test.add("stuff6");
        holiday.add(test);
        ArrayList<String> test1 = new ArrayList<>();
        test1.add("stuff11");
        test1.add("stuff12");
        test1.add("stuff13");
        test1.add("stuff14");
        test1.add("stuff15");
        test1.add("stuff16");
        holiday.add(test1);


        ArrayList<ArrayList> holidayScore = new ArrayList<>(); // creates new arraylist to hold the 6 attributes and their value points to be used in recommendation score
        //database information
        /*boolean condition = false;
        String[] select = null;
        String[] from = null;
        String[] where = null;
        holiday = database.getData(condition, select, from, where);*/

        //Loops through the holiday arraylist which contains all the holidays and adds the attributes that we need to the new one along with default points
        for(int i = 0; i < holiday.size(); i++){
            holidayScore.add(new ArrayList());
            holidayScore.get(i).add(holiday.get(i).get(0));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(1));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(2));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(3));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(4));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(5));
            holidayScore.get(i).add("0");
        }

        //Loops through the holidayScore arraylist checking the attributes against the valuePoints array list and 
        //modifying the value points where the two attribute values are equivalent in both arraylists
        //each if statement checks that each attrbute value is equivalent, successful matches record the points from valuePoints into holidayScore
        for(int j = 0; j < holidayScore.size(); j++){

            if(holidayScore.get(j).get(0) == valuePoints.get(j).get(0)){
                holidayScore.get(j).set(1, valuePoints.get(j).get(1));
            }

            if(holidayScore.get(j).get(2) == valuePoints.get(j).get(2)){
                holidayScore.get(j).set(3, valuePoints.get(j).get(3));
            }

            if(holidayScore.get(j).get(4) == valuePoints.get(j).get(4)){
                holidayScore.get(j).set(5, valuePoints.get(j).get(5));
            }

            if(holidayScore.get(j).get(6) == valuePoints.get(j).get(6)){
                holidayScore.get(j).set(7, valuePoints.get(j).get(7));
            }

            if(holidayScore.get(j).get(8) == valuePoints.get(j).get(8)){
                holidayScore.get(j).set(9, valuePoints.get(j).get(9));
            }

            if(holidayScore.get(j).get(10) == valuePoints.get(j).get(10)){
                holidayScore.get(j).set(11, valuePoints.get(j).get(11));
            }  
        }

        System.out.println(holiday);
        System.out.println("----------------------------------------------------");
        System.out.println(holidayScore);
        return holidayScore;
}

public static void recommendation(ArrayList<Double> weightedAttr, ArrayList<ArrayList> holidayScores){

        //each variable holds the current value points for that attribute value
        double countryValue;
        double durationValue;
        double accTypeValue;
        double holSubTypeValue;
        double weatherValue;
        double pricePPValue;
        double recScore;

        //Loops through the holidayScores arraylist convert the value points into double which is multiplied by the appropriate attribute weighting
        //this gives a decimal score for each attribute which is summed up for the final recommendation score and added to the end of the arraylist
        for(int k = 0; k < holidayScores.size(); k++){
            countryValue = Double.parseDouble(holidayScores.get(k).get(1).toString());
            durationValue = Double.parseDouble(holidayScores.get(k).get(3).toString());
            accTypeValue = Double.parseDouble(holidayScores.get(k).get(5).toString());
            holSubTypeValue = Double.parseDouble(holidayScores.get(k).get(7).toString());
            weatherValue = Double.parseDouble(holidayScores.get(k).get(9).toString());
            pricePPValue = Double.parseDouble(holidayScores.get(k).get(11).toString());
            recScore = (countryValue * weightedAttr.get(0));
            recScore = recScore + (durationValue * weightedAttr.get(1));
            recScore = recScore + (accTypeValue * weightedAttr.get(2));
            recScore = recScore + (holSubTypeValue * weightedAttr.get(3));
            recScore = recScore + (weatherValue * weightedAttr.get(4));
            recScore = recScore + (pricePPValue * weightedAttr.get(5));
            holidayScores.get(k).add(recScore);
        }

        System.out.println(holidayScores);
}

}

You should implement an object, that holds all the values in of your arraylist (inner). Then you can easily implement Comparable that checks for the recommendationScore .

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