Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java按列表类型排序对象属性_Java_Sorting_Guava - Fatal编程技术网

Java按列表类型排序对象属性

Java按列表类型排序对象属性,java,sorting,guava,Java,Sorting,Guava,我有以下目标: public class Shipping { String name; List<Method> methods; } public class Method { String serviceType; String cost; } 将按如下方式排序: shipping: "Josh" with 2 methods: "Premium","5" and "Basic","3" shopping: "Nash" with 2 met

我有以下目标:

public class Shipping {
    String name;
    List<Method> methods;
}

public class Method {
    String serviceType;
    String cost;
}
将按如下方式排序:

shipping: "Josh" with 2 methods: "Premium","5" and "Basic","3"
shopping: "Nash" with 2 methods: "Prem", "7" and "Base","2"
shopping: "Nash" with 2 methods: "Base","2" and "Prem", "7"
shopping: "Josh" with 2 methods: "Basic","3" and "Premium","5"
我需要它将成本最低的方法作为第一个方法返回,同时还需要将成本最低的方法作为第一个方法进行排序

最好的方法是什么?如果Java8有更好的解决方案,并且有
guava
library,我就使用它

编辑:
成本是一个浮点数。我需要将其保留为字符串,因为它是传递给REST api的对象,不希望客户端解析它。

您需要comparator或Comparable实现方法类,如:

public class Method implements Comparable<Method> {
    public int compareTo(Method thatMethod) {
        return Integer.compare(Integer.parseInt(this.cost), Integer.parseInt(thatMethod.getCost()));//if you need name then you could do this.cost.compareTo(thatMethod.getServiceType()); assuming serviceType can never be null 
    }
}

您需要一个comparator或实现Comparable for Method类,如:

public class Method implements Comparable<Method> {
    public int compareTo(Method thatMethod) {
        return Integer.compare(Integer.parseInt(this.cost), Integer.parseInt(thatMethod.getCost()));//if you need name then you could do this.cost.compareTo(thatMethod.getServiceType()); assuming serviceType can never be null 
    }
}

我建议您阅读Java的订购。您的需求似乎表明您希望按其
方法对每个
配送
实例进行排序,并且在其他地方您希望对希望按字母顺序排序的
配送
实例集合进行排序,但这与您编写的内容并不完全一致


在任何情况下,阅读本教程后都可以直接执行此操作。总之,您可以使用
比较器
或通过实现
Comparable
,并在数据集上简单地调用Collections.sort(…)。

我建议您阅读Java的排序。您的需求似乎表明您希望按其
方法对每个
配送
实例进行排序,并且在其他地方您希望对希望按字母顺序排序的
配送
实例集合进行排序,但这与您编写的内容并不完全一致


在任何情况下,阅读本教程后都可以直接执行此操作。总之,您可以使用
比较器
或通过实现
Compariable
,并简单地调用数据集上的Collections.sort(…)。

您可以首先对
发货
列表中每个
发货
实例的
方法
字段进行排序,然后根据每个实例的
方法的第一个元素对
发货
列表进行排序:

for (Shipping shipping : shippings)
    shipping.methods.sort((m1, m2) -> Integer.compare(m1.cost, m2.cost));

shippings.sort((s1, s2) -> 
  Integer.compare(s1.methods.get(0).cost, s2.methods.get(0).cost));

您可能需要做一些额外的工作,将成本转换为整数,但总体思路是相同的。

您可以首先对
发货列表中每个
发货
实例的
方法
字段进行排序,然后根据每个实例的
方法的第一个元素对
发货
列表进行排序:

for (Shipping shipping : shippings)
    shipping.methods.sort((m1, m2) -> Integer.compare(m1.cost, m2.cost));

shippings.sort((s1, s2) -> 
  Integer.compare(s1.methods.get(0).cost, s2.methods.get(0).cost));

您可能需要做一些额外的工作,将成本转换为整数,但总体思路是相同的。

您可以定义新的
比较器来定义排序标准,如下所示:

Comparator<Shipping> shippingComparator = new Comparator<Shipping>{
public int compare(Shipping obj1, Shipping obj2) {
    //your rules for comparing Shipping1, Shipping 2 goes here
    //return -1 when obj1 should be before obj2
    //return 1 when obj1 should be after obj2
    //return 0 when obj1 is equal to obj2 and relative position doesnt matter
} 
Comparator shippingComparator=新的比较器{
公共整数比较(装运obj1、装运obj2){
//这里是比较Shipping1和Shipping2的规则
//当obj1应在obj2之前时返回-1
//当obj1应在obj2之后时返回1
//当obj1等于obj2且相对位置无关紧要时,返回0
} 
然后使用此比较器对列表进行排序:

ArrayList<Shipping> shippings;
//populate List
Collections.sort(shippings, shippingComparator );
ArrayList发货;
//填充列表
Collections.sort(shippings、shippingComparator);

您可以定义新的
比较器来定义您的排序标准,如下所示:

Comparator<Shipping> shippingComparator = new Comparator<Shipping>{
public int compare(Shipping obj1, Shipping obj2) {
    //your rules for comparing Shipping1, Shipping 2 goes here
    //return -1 when obj1 should be before obj2
    //return 1 when obj1 should be after obj2
    //return 0 when obj1 is equal to obj2 and relative position doesnt matter
} 
Comparator shippingComparator=新的比较器{
公共整数比较(装运obj1、装运obj2){
//这里是比较Shipping1和Shipping2的规则
//当obj1应在obj2之前时返回-1
//当obj1应在obj2之后时返回1
//当obj1等于obj2且相对位置无关紧要时,返回0
} 
然后使用此比较器对列表进行排序:

ArrayList<Shipping> shippings;
//populate List
Collections.sort(shippings, shippingComparator );
ArrayList发货;
//填充列表
Collections.sort(shippings、shippingComparator);

我们假设所有发货至少有一种方法。因此,您希望发货的方法按成本排序。因此,让我们这样做:

shippings.forEach(shipping -> {
    shipping.getMethods().sort(Comparator.comparing(Method::getCost));
});
然后,您希望发货列表按其方法的最低成本进行排序。最低成本是第一种方法的成本,因为它们现在已排序:

shippings.sort(Comparator.comparing(shipping -> shipping.getMethods().get(0).getCost()));

请注意,这假设您希望按字典顺序比较成本。如果,如我所怀疑的,成本事实上是一个数字,那么它应该存储在Method类中,而不是作为字符串。因此,将其设置为整数或BigDecimal,或任何适当的类型。

我们将假设所有装运至少有一个方法。因此您希望按成本对发货方法进行排序。因此,让我们这样做:

shippings.forEach(shipping -> {
    shipping.getMethods().sort(Comparator.comparing(Method::getCost));
});
然后,您希望发货列表按其方法的最低成本进行排序。最低成本是第一种方法的成本,因为它们现在已排序:

shippings.sort(Comparator.comparing(shipping -> shipping.getMethods().get(0).getCost()));

请注意,这假设您希望按字典顺序比较成本。如果我怀疑成本实际上是一个数字,那么它应该作为数字存储在方法类中,而不是作为字符串。因此,请将其设置为整数或BigDecimal,或任何适当的类型。

因为java中的字符串按字典顺序比较,所以可以使用also避免解析为整数,并使用
this.cost.compareTo(other.getCost())
@francescoforesti不,数字不需要字典排序。字典排序的意思是,例如
9
大于
10
@Radiodef你说得对!但是,真正的问题是
String
不是那种数据的正确类型..谢谢。如果成本实际上是浮动的呢?它不只是排序吗e方法?在比较java中的methodsSince字符串时,我需要对发货进行实际排序。如果要按字典顺序进行比较,还可以避免解析为整数,并使用
this.cost.compareTo(other.getCost())
@francescoforesti不,数字不需要字典排序。字典排序的意思是,例如
9
大于
10
@Radiodef你是对的!但是,真正的问题是
字符串
不是该亲属的正确类型