根据具有特殊条件的多个字段对对象列表进行排序

我正在尝试根据两个字段 [ Name , Old name ] 按升序对对象列表进行排序,其中Name字段值可以是破折号(“-”),如果名称是破折号,那么它将被添加到列表的末尾. [像下面的顺序:名称:旧名称]


SENSITIVE: COMP Operations: COMP Operations

SENSITIVE: Court procedural documents: Court procedural documents

SENSITIVE: Staff matter: Staff Matter

SPECIAL HANDLING: ETS Critical: ETS Critical

-: ETS Limited

-: EU Satellite Navigation matters

-: Limited ETS Joint Procurement

到目前为止,我得到了以下结果:


-: ETS Limited

-: EU Satellite Navigation matters

-: Limited ETS Joint Procurement

SENSITIVE: COMP Operations: COMP Operations

SENSITIVE: Court procedural documents: Court procedural documents

SENSITIVE: Staff matter: Staff Matter

SPECIAL HANDLING: ETS Critical: ETS Critical

领域模型:


class Marking {

    String name;

    String oldName;


    public Marking(String name, String oldName) {

        this.name = name;

        this.oldName = oldName;

    }


    public String getName() {

        return name;

    }


    public void setName(String name) {

        this.name = name;

    }


    public String getOldName() {

        return oldName;

    }


    public void setOldName(String oldName) {

        this.oldName = oldName;

    }


    @Override

    public String toString() {

        return "Marking [name=" + name + ", oldName=" + oldName + "]";

    }

}

和解决方案类:


public class Solution {

    public static void main(String[] args) {

        List<Marking> markings = new ArrayList<>();

        markings.add(new Marking("-", "Limited ETS Joint Procurement"));

        markings.add(new Marking("-", "EU Satellite Navigation matters"));

        markings.add(new Marking("SENSITIVE: Court procedural documents", "Court procedural documents"));

        markings.add(new Marking("SENSITIVE: COMP Operations", "COMP Operations"));

        markings.add(new Marking("-", "ETS Limited"));

        markings.add(new Marking("SENSITIVE: Staff matter", "Staff Matter"));

        markings.add(new Marking("SPECIAL HANDLING: ETS Critical", "ETS Critical"));



任何人都可以给我一些建议,如何将破折号名称对象添加到列表的末尾,其中旧名称应按升序排列。


海绵宝宝撒
浏览 129回答 2
2回答

吃鸡游戏

这是我很快想到的一个选项:&nbsp; Collections.sort(markings, (o1, o2) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int value = o1.getName().compareToIgnoreCase(o2.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(value == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return o1.getOldName().compareToIgnoreCase(o2.getOldName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(o1.getName().equals("-")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(o2.getName().equals("-")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; &nbsp; &nbsp; });

缥缈止盈

这是一个建议。顺序很好,我们所做的就是取出所有以“-”开头的元素并将它们从列表中删除,然后将它们添加回末尾。这是低效的,但它是一种决心。&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<Marking> markings = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; markings.add(new Marking("-", "Limited ETS Joint Procurement"));&nbsp; &nbsp; &nbsp; &nbsp; markings.add(new Marking("-", "EU Satellite Navigation matters"));&nbsp; &nbsp; &nbsp; &nbsp; markings.add(new Marking("SENSITIVE: Court procedural documents", "Court procedural documents"));&nbsp; &nbsp; &nbsp; &nbsp; markings.add(new Marking("SENSITIVE: COMP Operations", "COMP Operations"));&nbsp; &nbsp; &nbsp; &nbsp; markings.add(new Marking("-", "ETS Limited"));&nbsp; &nbsp; &nbsp; &nbsp; markings.add(new Marking("SENSITIVE: Staff matter", "Staff Matter"));&nbsp; &nbsp; &nbsp; &nbsp; markings.add(new Marking("SPECIAL HANDLING: ETS Critical", "ETS Critical"));&nbsp; &nbsp; &nbsp; &nbsp; markings.sort((o1, o2) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int value = o1.getName().compareToIgnoreCase(o2.getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return o1.getOldName().compareToIgnoreCase(o2.getOldName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; List<Marking> dashed = markings.stream().filter(marking -> marking.name.startsWith("-") || marking.oldName.startsWith("-"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp; markings.removeAll(dashed);&nbsp; &nbsp; &nbsp; &nbsp; markings.addAll(dashed);&nbsp; &nbsp; &nbsp; &nbsp; for (Marking marking : markings) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(marking.getName() + ": " + marking.getOldName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java