Sorting a double value of an object within an arrayList

后端 未结 5 962
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 15:07

I\'m trying to sort my custom class chromosome by the value of their score attribute which is a double. These chromosomes are stored within an ArrayList. I know I have to us

5条回答
  •  清酒与你
    2020-12-28 15:23

    Why not use a PriorityQueue with a Comparator like this:

    // your code
    PriorityQueue entries = new PriorityQueue(1, new Comparator () {
        @Override
        public int compare(Chromosome arg0, Chromosome arg1) {
            return (Double)(arg1.getScore()).compareTo((Double)arg0.getScore());
        }
    });
    entries.addAll(arrayListOfChromosomes);
    // your code
    

    The priority queue will then keep your data structure in sorted order.

提交回复
热议问题