Java 对简单的对象列表进行排序

Java 对简单的对象列表进行排序,java,Java,我从数据库中获得两个对象,需要按行数对其进行排序(在本例中,它已经排序) 我需要返回带有testFlow.name的字符串[] private int id; private String name; private TestCase testCase; private int rowNumber; private TestType testType; private String params; private String creationDate; private String creat

我从数据库中获得两个对象,需要按行数对其进行排序(在本例中,它已经排序)

我需要返回带有
testFlow.name的字符串[]

private int id;
private String name;
private TestCase testCase;
private int rowNumber;
private TestType testType;
private String params;
private String creationDate;
private String createdBy;

List<TestFlow> testFlow = hibernateSession.createQuery("FROM TestFlow WHERE name= :testFlowName").setParameter("testFlowName",testFlowName).list();
//Sort it and send the testCase flow by order
Collections.sort(testFlow)
private int-id;
私有字符串名称;
私有测试用例测试用例;
私有整数行数;
私有TestType TestType;
私有字符串参数;
私有字符串创建日期;
创建的私有字符串;
List testFlow=hibernateSession.createQuery(“来自testFlow,其中name=:testFlowName”).setParameter(“testFlowName”,testFlowName).List();
//排序并按顺序发送测试用例流
Collections.sort(testFlow)

您需要一个比较器,它可以根据您想要的标准进行排序:

Comparator<? super TestFlow> comparator = new Comparator<TestFlow>() {
    @Override
    public int compare(TestFlow o1, TestFlow o2) {
        return o1.getRowNumber().compareTo(o2.getRowNumber());
    }
};
testFlow.sort(comparator);

因为rowNumber似乎是对象的一个字段,所以我假设它也是表中的一列。在这种情况下,您只需将“ORDER BY rowNumber”添加到HQL查询中,而不使用集合。sort

请发布语法完整的类定义,而不仅仅是一组字段。如果你只想对两个物体进行分类,你有什么困难?你自己都试过什么了?这是一个老兄。我有一个
列表
,我需要按rowNumber@sda应该很容易编写一个可编译的示例。
testFlow.sort(Comparator.comparing(TestFlow::getRowNumber));