代码之家  ›  专栏  ›  技术社区  ›  ssl

Java按名称对对象进行排序,但一个对象应始终位于顶部

  •  3
  • ssl  · 技术社区  · 7 年前
    class Manager
    {
        private int id;
        private String name;
    
        public Manager(int id, String name)
        {
          this.id = id;
          this.name = name;
        }
    
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public int getId() { return id; }
        public void setId(int id) { this.id = id; }
    }
    
    List<Manager> names = new ArrayList<>();
    names.add(new Manager(1, "Robert"));
    names.add(new Manager(2, "Paul"));
    names.add(new Manager(3, "None"));
    names.add(new Manager(4, "Nancy"));
    names.add(new Manager(4, "Nancy"));
    
    names.stream().sorted(
                    Comparator.comparing(n->n.getName())).collect(Collectors.toList());
    
    
    List<String> names = new List<String>();
    names.add("Robert");
    names.add("Paul");
    names.add("None");
    names.add("Nancy");
    names.add("Nancy");
    
    
    names.stream().sorted().collect(Collectors.toList());
    

    我需要对Java 8中的字符串列表进行排序,我可以使用下面的逻辑轻松地进行排序,但我希望始终在顶部显示下面列表中的None。结果必然是。我需要获取管理器对象列表的排序顺序

    None
    Jhon
    Nancy
    Paul
    

    我的代码:

    List<String> names = new List<String>();
    names.add("Robert");
    names.add("Paul");
    names.add("None");
    names.add("Nancy");
    names.add("Jhon");
    
    
    names.stream().sorted().collect(Collectors.toList());
    

    字符串必须始终放在第一位。

    7 回复  |  直到 7 年前
        1
  •  6
  •   Holger    7 年前

    您可以使用

    List<Manager> sorted = names.stream()
        .sorted(Comparator.comparing(Manager::getName,
                  Comparator.comparing((String s) -> !s.equals("None"))
                            .thenComparing(Comparator.naturalOrder())))
        .collect(Collectors.toList());
    

    排序到新列表或使用

    names.sort(Comparator.comparing(Manager::getName,
                  Comparator.comparing((String s) -> !s.equals("None"))
                            .thenComparing(Comparator.naturalOrder())));
    

    要就地排序,可以使用可变列表,如 ArrayList .

        2
  •  3
  •   Ousmane D.    7 年前

    您可以将比较器对象传递给 sorted 定义标准的中间方法如下:

    List<String> resultSet = 
             names.stream()
                  .sorted((e, a) -> "None".equals(e) ? -1:
                                  "None".equals(a) ? 1 : e.compareTo(a))
                  .collect(Collectors.toList());
    
        3
  •  2
  •   Sergey Kalinichenko    7 年前

    一种方法是过滤掉 "None" ,并将其预挂起到排序列表:

    List<String> ordered = Stream.concat(
        names.stream().filter(s -> s.equals("None"))
    ,   names.stream().filter(s -> !s.equals("None")).sorted()
    ).collect(Collectors.toList());
    

    Demo.

        4
  •  0
  •   Saravana    7 年前

    使用 sorted partitioningBy

        Map<Boolean, List<String>> partition = names
                .stream()
                .sorted()
                .collect(Collectors.partitioningBy("None"::equals));
        System.out.println(partition);
    
        List<String> noneFirst = new ArrayList<>(partition.get(true));
        noneFirst.addAll(partition.get(false));
        System.out.println(noneFirst);
    

    输出

    [None, Jhon, Nancy, Paul, Robert]
    
        5
  •  0
  •   user6874700 user6874700    7 年前
    List<String> sorted = names.stream()
                    .sorted((o1, o2) -> o1 == null ?
                            -1 :
                            o2 == null ?
                                    1 :
                                    o1.equals("None") ?
                                            -1 :
                                            o2.equals("None") ?
                                                    1 :
                                                    o1.compareTo(o2))
                    .collect(Collectors.toList());
    
        6
  •  0
  •   Valentin Ruano    7 年前

    这是我的贡献:尽量使其格式/外观尽可能好。 请注意,这将失败。

     List<String> unsorted = ... what-ever ...;
     // ... 
     List<String> sorted = unsorted.stream()
        .filter(Objects::nonNull) // optional: get rid of nulls before an exception is thrown bellow.
        .sorted((a, b) -> Objects.equals(a, b) ? 0 
                      : "None".equals(a) ? -1
                      : "None".equals(b) ? 1 
                      : a.compareTo(b))
        .distinct()  // optional: if you want to get rid of repeats.
        .collect(Collectors.toList());
    
        7
  •  -1
  •   pvpkiran    7 年前

    看看这个

       names.stream()
                            .sorted((o1, o2) ->
                                    {
                                        if("None".equals(o1.getName()) && "None".equals(o2.getName())) return 0;
                                        return "None".equals(o1.getName()) ? -1:
                                                "None".equals(o2.getName()) ? 1 : o1.getName().compareTo(o2.getName());
                                    }
                            ).collect(Collectors.toList());