Java 使用许多属性的对象 Comparable
Java Object Comparable using many attributes
我有一些订单要在配送路线中装卸,一条路线可以分配一个 OrderDocument
给它装货、卸货或两者兼而有之。
每个订单文件都按分配的路线中的索引排序。所以 class 看起来像这样:
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
}
然后我有路线 class:
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 中,我需要此列表按 load/unload 索引排序。
实际上我有以下 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;
}
}
我有一些订单要在配送路线中装卸,一条路线可以分配一个 OrderDocument
给它装货、卸货或两者兼而有之。
每个订单文件都按分配的路线中的索引排序。所以 class 看起来像这样:
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
}
然后我有路线 class:
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 中,我需要此列表按 load/unload 索引排序。
实际上我有以下 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;
}
}