java - Java根据交付ID在国际交付列表中升序排序
问题描述
这是在java中的类应用程序中编码。此类关于在本地和海外交付的应用程序。所以,我需要对海外身份证号进行升序排序。
import java.util.ArrayList;
import java.util.*;
public class Delivery App
{
public static void main(String[] args)
{
ArrayList myDeliveryList = new ArrayList();
Delivery d;
d = new Delivery("Hassan Ali", "R4321", 'L', 93.59);
myDeliveryList.add(d);
d = new Delivery("Ahmad Kassim", "M1131", 'O', 11.56);
myDeliveryList.add(d);
d = new Delivery("Atikah Hussin", "S4321", 'L', 81.24);
myDeliveryList.add(d);
d = new Delivery("Umi Rina", "P9087", 'L', 27.98);
myDeliveryList.add(d);
d = new Delivery("Lam Ah Soon", "R2342", 'O', 25.67);
myDeliveryList.add(d);
d = new Delivery("Zaidi Zain", "N8876", 'O', 51.99);
myDeliveryList.add(d);
d = new Delivery("Mohd. Mahmud", "D3321", 'L', 13.32);
myDeliveryList.add(d);
d = new Delivery("Gopal Samy", "S1134", 'L', 80.87);
myDeliveryList.add(d);
d = new Delivery("Ina Naim", "P3456", 'O', 32.67);
myDeliveryList.add(d);
d = new Delivery("Suffian Suhaimi", "P3217", 'L', 79.88);
myDeliveryList.add(d);
for(int i=0; i<myDeliveryList.size(); i++)
{
d= (Delivery)myDeliveryList.get(i);
//to display detail about hassan ali only
if (d.getCN().equalsIgnoreCase("hassan ali"))
{
System.out.println(d.toString());
}
}
//to make a new ArrayList for local and oversea
ArrayList LocalDeliveryList = new ArrayList();
ArrayList InternationalDeliveryList = new ArrayList();
for (int i=0; i<myDeliveryList.size(); i++)
{
d= (Delivery)myDeliveryList.get(i);
//to add all destination in their own categorize
if (d.getD() == 'L')
{
LocalDeliveryList.add(d);
}
else
{
InternationalDeliveryList.add(d);
}
}
System.out.println("\nPackage delivered to local destination : "+LocalDeliveryList.size());
System.out.println(" Package delivered to international destination: "+InternationalDeliveryList.size());
double Ltotal=0.00;
double Ototal=0.00;
double priceL;
double priceO;
//to calculate the total for one package either in local and oversea
for (int i=0; i<myDeliveryList.size(); i++)
{
d= (Delivery)myDeliveryList.get(i);
if (d.getD() == 'L')
{
priceL = 7.00;
Ltotal = priceL*d.getW();
}
else
{
priceO = 11.00;
Ototal = priceO*d.getW();
}
}
我尝试使用它对其进行排序,collections.sort()
但它无法读取。你们能帮我按身份证号码对国际快递进行分类吗?我只想对国际送货清单中的身份证号进行排序。
解决方案
这可能对您有用:
InternationalDeliveryList.sort(Comparator.comparing(Delivery::getId));
getId
- 是要排序的字段的 getter 方法
在创建 ArrayList 时还要指定类型。
ArrayList<Delivery> InternationalDeliveryList = new ArrayList();
完整代码:
import java.util.ArrayList;import java.util.Comparator;
public class Delivery {
private String cN;
private char d;
private double w;
private String number;
Delivery(String cN,String number,char d,double w){
this.cN=cN;
this.number=number;
this.d=d;
this.w=w;
}
public double getW() {
return w;
}
public void setW(double w) {
this.w = w;
}
public char getD() {
return d;
}
public void setD(char d) {
this.d = d;
}
public String getCN() {
return cN;
}
public void setCN(String cN) {
this.cN = cN;
}
public static void main(String[] args) {
ArrayList<Delivery> myDeliveryList = new ArrayList();
Delivery d;
d = new Delivery("Hassan Ali", "R4321", 'L', 93.59);
myDeliveryList.add(d);
d = new Delivery("Ahmad Kassim", "M1131", 'O', 11.56);
myDeliveryList.add(d);
d = new Delivery("Atikah Hussin", "S4321", 'L', 81.24);
myDeliveryList.add(d);
d = new Delivery("Umi Rina", "P9087", 'L', 27.98);
myDeliveryList.add(d);
d = new Delivery("Lam Ah Soon", "R2342", 'O', 25.67);
myDeliveryList.add(d);
d = new Delivery("Zaidi Zain", "N8876", 'O', 51.99);
myDeliveryList.add(d);
d = new Delivery("Mohd. Mahmud", "D3321", 'L', 13.32);
myDeliveryList.add(d);
d = new Delivery("Gopal Samy", "S1134", 'L', 80.87);
myDeliveryList.add(d);
d = new Delivery("Ina Naim", "P3456", 'O', 32.67);
myDeliveryList.add(d);
d = new Delivery("Suffian Suhaimi", "P3217", 'L', 79.88);
myDeliveryList.add(d);
for (int i = 0; i < myDeliveryList.size(); i++) {
d = (Delivery) myDeliveryList.get(i);
// to display detail about hassan ali only
if (d.getCN().equalsIgnoreCase("hassan ali")) {
System.out.println(d.toString());
}
}
// to make a new ArrayList for local and oversea
ArrayList LocalDeliveryList = new ArrayList();
ArrayList<Delivery> InternationalDeliveryList = new ArrayList();
for (int i = 0; i < myDeliveryList.size(); i++) {
d = (Delivery) myDeliveryList.get(i);
// to add all destination in their own categorize
if (d.getD() == 'L') {
LocalDeliveryList.add(d);
} else {
InternationalDeliveryList.add(d);
}
}
System.out.println("\nPackage delivered to local destination : " + LocalDeliveryList.size());
System.out.println(" Package delivered to international destination: " + InternationalDeliveryList.size());
InternationalDeliveryList.sort(Comparator.comparing(Delivery::getW));
for(Delivery delivery: InternationalDeliveryList) {
System.out.println(delivery.getW());
}
double Ltotal = 0.00;
double Ototal = 0.00;
double priceL;
double priceO;
// to calculate the total for one package either in local and oversea
for (int i = 0; i < myDeliveryList.size(); i++) {
d = (Delivery) myDeliveryList.get(i);
if (d.getD() == 'L') {
priceL = 7.00;
Ltotal = priceL * d.getW();
} else {
priceO = 11.00;
Ototal = priceO * d.getW();
}
}
}
}
推荐阅读
- c# - 带有 WPF 控件的 Azure Function V1
- python - 直接从 Azure 数据工厂执行 egg
- java - Java类型的方法未定义的问题
- javascript - 在一张图中绘制多个折线图时,ChartJS 绘图不正确
- java - Java 特殊字符编码
- javascript - 单击时隐藏可折叠的 Bootstrap 4 导航栏
- kubernetes - 如何配置 Mattermost 插件
- r - R selenium webdriver 即使在等待并向下滚动后也不会加载元素
- heroku - 在 Heroku 上部署托管的深度学习模型?
- sql - 从 SQL Server 中的两个表中获取不匹配的行