排序-如何在Java中使用Collections.sort()?

我有一个对象Recipe实现了getRecipesSort()

public int compareTo(Recipe otherRecipe) {
    return this.inputRecipeName.compareTo(otherRecipe.inputRecipeName);
}

我这样做是为了能够按以下方法按字母顺序对3007478579572762769792进行排序:

public static Collection<Recipe> getRecipes(){
    List<Recipe> recipes = new ArrayList<Recipe>(RECIPE_MAP.values());
    Collections.sort(recipes);
    return recipes;
}

但是现在,以另一种方法命名为3007478579572762769792,我想对同一列表进行排序,但以数字方式比较包含ID的变量。 更糟的是,ID字段的类型为String

如何使用Collections.sort()在Java中执行排序?

jsfrocha asked 2020-08-12T01:56:50Z
5个解决方案
59 votes

使用此方法Collections.sort(List,Comparator)。 实施比较器并将其传递给Comparator

class RecipeCompare implements Comparator<Recipe> {

    @Override
    public int compare(Recipe o1, Recipe o2) {
        // write comparison logic here like below , it's just a sample
        return o1.getID().compareTo(o2.getID());
    }
}

然后使用Comparator作为

Collections.sort(recipes,new RecipeCompare());
NINCOMPOOP answered 2020-08-12T01:57:01Z
23 votes

使用Lambda表达式可以使NINCOMPOOP给出的答案更简单:

Collections.sort(recipes, (Recipe r1, Recipe r2) ->
r1.getID().compareTo(r2.getID()));

Java 8之后还引入了Comparator接口中的比较器构造方法。 使用这些,可以进一步将其减少为1:

recipes.sort(comparingInt(Recipe::getId));

1 Bloch,J.有效的Java(第3版)。 2018年,项目42,第43页。 194。

Gyanesh Sharma answered 2020-08-12T01:57:34Z
6 votes

创建一个比较器,该比较器在其构造函数中接受比较模式,并根据您的要求为不同的场景传递不同的模式

public class RecipeComparator implements Comparator<Recipe> {

public static final int COMPARE_BY_ID = 0;
public static final int COMPARE_BY_NAME = 1;

private int compare_mode = COMPARE_BY_NAME;

public RecipeComparator() {
}

public RecipeComparator(int compare_mode) {
    this.compare_mode = compare_mode;
}

@Override
public int compare(Recipe o1, Recipe o2) {
    switch (compare_mode) {
    case COMPARE_BY_ID:
        return o1.getId().compareTo(o2.getId());
    default:
        return o1.getInputRecipeName().compareTo(o2.getInputRecipeName());
    }
}

}

实际上,您需要单独处理数字,请在下面检查

public static void main(String[] args) {
    String string1 = "1";
    String string2 = "2";
    String string11 = "11";

    System.out.println(string1.compareTo(string2)); 
    System.out.println(string2.compareTo(string11));// expected -1 returns 1
   // to compare numbers you actually need to do something like this

    int number2 = Integer.valueOf(string1);
    int number11 = Integer.valueOf(string11);

    int compareTo = number2 > number11 ? 1 : (number2 < number11 ? -1 : 0) ;
    System.out.println(compareTo);// prints -1
}
threadfin answered 2020-08-12T01:58:03Z
3 votes

当您想以自然顺序以外的其他方式排序时,请使用接受Comparator的方法。

Collections.sort(列表,比较器)

John B answered 2020-08-12T01:58:27Z
0 votes

按升序对未排序的哈希图进行排序。

// Sorting the list based on values
Collections.sort(list, new Comparator<Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) 
{
                return o2.getValue().compareTo(o1.getValue());
        }
    });

    // Maintaining insertion order with the help of LinkedList
    Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
    for (Entry<String, Integer> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }
Kashan answered 2020-08-12T01:58:47Z
translate from https://stackoverflow.com:/questions/16425127/how-to-use-collections-sort-in-java