简体   繁体   English

基于对象的ArrayList对值进行排序

[英]Sorting Values based on ArrayList of Objects

I stuck with comparing values. 我坚持比较价值观。 Array List consists of two objects Name and Percentage. 数组列表由两个对象Name和Percentage组成。

public class DataModel {

    private String name;
    private String dataUsagePercent;
     int color;
}

and array list is 和数组列表是

 List<DataModel> usageList;


 Collections.sort(usageList, new Comparator<DataModel>(){
        public int compare(DataModel d1, DataModel d2){

            return (d1.getDataUsagePercent()) - Integer.parseInt(d2.getDataUsagePercent()));
        }

    });

Example : 示例:

dataUsagePercent values in a arraylist is 2, 10, 40, 30, 50, 60, 90, 65,55,100. arriylist中的dataUsagePercent值为2, 10, 40, 30, 50, 60, 90, 65,55,100.

i have to compare the values of dataUsagePercent . 我必须比较dataUsagePercent的值。 lowest should come at 0th position in arraylist and the top four categories that make up the highest value of dataUsagePercent. 最低位置应位于arraylist中的第0位,前4位类别构成dataUsagePercent的最高值。 Additionally, the six remaining values (that make up the lowest percentage of usage) are combined into a single percentage. 此外,剩余的六个值(构成最低使用百分比)合并为一个百分比。

Output for above values after comparing list should consists of : 比较列表后的上述值的输出应包括:

2,10+30+40+50+55+60,65,90,100 means 2, 245,65,90,100

final list size should be always 5 only and name value has no impact 最终列表大小应始终为5,名称值不会产生任何影响

Edit: Missed 60. add to combined value 编辑:错过60.添加到组合值

I think your list data type need to change as List<DataUsageModel> usageList; 我认为您的列表数据类型需要更改为List<DataUsageModel> usageList; to compare dataUsagePercent values you can use Collections.sort(usageList); 比较dataUsagePercent值你可以使用Collections.sort(usageList); or you can use priority queue 或者您可以使用优先级队列

First, order the list by dataUsagePercent : 首先,按dataUsagePercent对列表进行dataUsagePercent

usageList.sort(
    Comparator.comparing(
        DataModel::getDataUsagePercent, 
        Comparator.comparingInt(Integer::parseInt)));

Here I'm using List.sort and Comparator.comparing methods. 这里我使用的是List.sortComparator.comparing方法。

Then, I'd use a common for loop to do the grouping, taking care of the index: 然后,我将使用一个通用的for循环来进行分组,处理索引:

int[] topFive = new int[5];

int size = usageList.size();
for (int i = 0; i < size; i++) {
    int value = Integer.parseInt(usageList.get(i).getDataUsagePercent());
    if (i == 0) {
        topFive[0] = value; // lowest value at the beginning
    } else if (i > 0 && i < size - 3) {
        topFive[1] += value; // accumulate intermediate values in 2nd position
    } else {
        topFive[i - size + 5] = value; // top values in the last 3 positions
    }
}

The output is [2, 245, 65, 90, 100] . 输出为[2, 245, 65, 90, 100]

First I would sort the list: 首先,我会对列表进行排序:

Collections.sort(usageList, new Comparator<DataModel>(){
    public int compare(DataModel d1, DataModel d2){
        return Integer.compare(d1.getDataUsagePercent(), d2.getDataUsagePercent());
    }
});
  • After that you can just take the first element and the last 3. 之后你可以拿第一个元素和最后一个元素。
  • Add the middle values together 将中间值一起添加
  • And create your result array 并创建结果数组

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

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