首页 > 解决方案 > 我如何在java中对这个自定义arraylist进行排序,按包装箱数的降序排列

问题描述

这是我的 Crackerpacker 的第一个类,我为 arraylist 创建了另一个类,然后将所有这些饼干包装器添加到该 arraylist 中,现在我想按 CrackerPackers 包装的盒子数量的降序对 arraylist 进行排序

public class CrackerPacker {
      private   String name;
      private int numberOfBoxes;

      public CrackerPacker (String name, int numberOfBoxes){
           this.name = name;
           this.numberOfBoxes = numberOfBoxes;
      }

        public int getNumberOfBoxes() {
            return numberOfBoxes;
        }

        public void setNumberOfBoxes(int numberOfBoxes) {
            this.numberOfBoxes = numberOfBoxes;
        }

        public String getName() {
            return name;
        enter code here
        }
        @Override
        public String toString() {
            return "CrackerPacker{" +
                    "name='" + name + '\'' +
                    ", numberOfBoxes=" + numberOfBoxes +
                    '}';
        }
        public double getwage() {
          if ( numberOfBoxes < 51 ) {
              return numberOfBoxes * 1.15;
          }
          else {
              double wage = (57.5 + ((numberOfBoxes - 50) * 1.25));
              return wage;

这是我创建的第二个类,其中我创建了一个数组列表以在其中添加所有的饼干包装器对象这个数组列表我想按饼干包装器包装的盒子数量的降序排序

import java.util.ArrayList;
import java.util.Collections;

public class Common {
    public static ArrayList<CrackerPacker> lectures = new ArrayList<>();

    int sum = 0;
    int boxes = 0;

    public int totalWage() {
        for (int i = 0; i < lectures.size(); i++) {
            sum += lectures.get(i).getwage();
        }
        return sum;
    }
    public int totalBoxes() {
        for (int i = 0; i < lectures.size(); i++){
            boxes += lectures.get(i).getNumberOfBoxes();

        }
    return boxes;

这是我的主要方法,我想按对象包装的盒子数量的降序对数组列表进行排序,请告诉我怎么做

  import java.util.Collections;

  public class Main {

    public static void main(String[] args) {
        CrackerPacker steve = new CrackerPacker("STEVE", 127);
        CrackerPacker gary = new CrackerPacker("Gary", 103);
        CrackerPacker tony = new CrackerPacker("tony", 473);
        CrackerPacker saad = new CrackerPacker("Saad", 129);
        CrackerPacker rubiya = new CrackerPacker("rubiya", 117);

        Common common = new Common();

        Common.lectures.add(steve);
        Common.lectures.add(gary);
        Common.lectures.add(tony);
        Common.lectures.add(saad);
        Common.lectures.add(rubiya);


        for ( CrackerPacker element : Common.lectures) {
            System.out.println(element);
        }
        System.out.println(common.totalWage());
        System.out.println(common.totalBoxes());

我想我已经使用了 compareto 方法,但我不知道如何实现它

标签: javasortingarraylistcompareto

解决方案


下面将numberOfBoxes传递CrackerPacker的对象与当前对象的对象进行比较。这将具有按降序对其进行排序的效果。

public class CrackerPacker implements Comparable<CrackerPacker> {
    @Override
    public int compareTo(CrackerPacker c) {
        return Integer.compare(c.numberOfBoxes, this.numberOfBoxes);
    }
}

从javadoc,

将此对象与指定对象进行比较以进行排序。返回负整数、零或正整数,因为此对象小于、等于或大于指定对象。

因此,当比较两个 CrackerPacker 对象时,我们正在反转整数比较检查以实现降序。

示例:比较 5 和 10,我们将 10 与 5 进行比较,因此Integer.compare返回 1(因为 10 > 5)。但从外部比较来看,我们说第一个对象大于第二个对象,因此它将出现在第二个对象之后(..10....5...)。这将按降序对列表进行排序。


推荐阅读