我有一些要在送货路线中装卸的订单,一个路线可以分配一个OrderDocument
以便装卸,或同时装两者。
每个订单文档都通过分配的路径中的索引进行排序。因此,该类如下所示:
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
或{{1 }}
主要问题
如果路由ID在load_id或unload_id中,则我需要按负载/卸载索引对该列表进行排序。
实际上,我有以下route_unload_id
方法,但是没有用。
Comparator
示例
Route-43中有4个订单,要加载2个,要卸载4个。
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;
}
}
按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)
的实际顺序:1、2、3、1
需要order_id
进行订购:1、3、2、1
答案 0 :(得分:1)
如果使用null
值(以及可为空的对应类型),则必须检查条件是否不比较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;
}
}