我有一些订单需要在配送 route 装载和卸载,一条路线可以指定一个 OrderDocument
供其装载、卸载或同时装载和卸载。
每个订单文档均按分配的 route 的索引进行排序。所以类看起来像这样:
class OrderDocument implements Comparable<ModelTask>{
int order_id; // The document ID
int route_load_id; // The route this order is assigned to be loaded
int route_unload_id; // The route this order is assigned to be loaded, may be same as route_load_id and viceversa
int route_load_index; // The index order in load route
int route_unload_index; // The index order in unload route
}
然后我有 Route 类:
class Route{
int id; // ID of route, this value should be in OrderDocument.route_load/unload_id
ArrayList<OrderDocument> orders; // **I WANT TO SORT THIS LIST**
...
因此,我有一个函数可以创建 Route 实例并将 OrderDocument 对象添加到其 orders
ArrayList 中,因此该列表应该具有 Route.id
位于 中的文档>route_load_id
或在 route_unload_id
主要问题
如果路线 id 在 load_id 或 unload_id 中,我需要按加载/卸载索引对这个列表进行排序。
实际上我有以下 Comparator
方法,但不起作用。
private class OrdersComparable implements Comparator<OrderDocument>{
@Override
public int compare(OrderDocument x, OrderDocument y) {
if (x.route_load_id == y.route_load_id)
return x.route_load_index - y.route_load_index;
else if (x.route_load_id == y.route_unload_id)
return x.route_load_index - y.route_unload_index;
else if (x.route_unload_id == y.route_load_id)
return x.route_unload_index - y.route_load_index;
else if (x.route_unload_id == y.route_unload_id)
return x.route_unload_index - y.route_unload_index;
else
return 0;
}
}
示例
43号公路上有4个订单,2个待装,4个待卸。
OrderDocument('order_id':1, 'route_load_id':43, 'route_unload_id':null, 'route_load_index':0, 'route_unload_index':null)
OrderDocument('order_id':2, 'route_load_id':43, 'route_unload_id':null, 'route_load_index':2, 'route_unload_index':null)
OrderDocument('order_id':3, 'route_load_id':null, 'route_unload_id':43, 'route_load_index':null, 'route_unload_index':1)
OrderDocument('order_id':1, 'route_load_id':null, 'route_unload_id':43, 'route_load_index':null, 'route_unload_index':3)
按 order_id
的实际订单:1, 2, 3, 1
按 order_id
要求的订单:1, 3, 2, 1
最佳答案
如果您使用 null
值(以及可空的相应类型),您必须检查您的条件是否比较空值,否则将满足不理想的条件:
class OrderDocument implements Comparable<ModelTask> {
int order_id; // The document ID
Integer route_load_id; // The route this order is assigned to be loaded
Integer route_unload_id; // The route this order is assigned to be loaded, may be same as route_load_id and viceversa
Integer route_load_index; // The index order in load route
Integer route_unload_index; // The index order in unload route
}
class OrdersComparable implements Comparator<OrderDocument> {
@Override
public int compare(OrderDocument x, OrderDocument y) {
if (x.route_load_id == y.route_load_id && x.route_load_id != null)
return Integer.compare(x.route_load_index, y.route_load_index);
if (x.route_load_id == y.route_unload_id && x.route_load_id != null)
return Integer.compare(x.route_load_index, y.route_unload_index);
if (x.route_unload_id == y.route_load_id && x.route_unload_id != null)
return Integer.compare(x.route_unload_index, y.route_load_index);
if (x.route_unload_id == y.route_unload_id && x.route_unload_id != null)
return Integer.compare(x.route_unload_index, y.route_unload_index);
return 0;
}
}
关于使用许多属性进行 Java 对象比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53218275/