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

时间:2018-01-10 16:08:23

标签: java

我从数据库中获取了两个对象,我需要通过rowNumber对它进行排序(在这种情况下,它已经订购了)http://prntscr.com/hykvqn

我需要使用testFlow.name

返回String []
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)

2 个答案:

答案 0 :(得分:1)

由于rowNumber似乎是您对象的一个​​字段,我认为它也是您表中的一列。在这种情况下,您只需添加&#34; ORDER BY rowNumber&#34;您的HQL查询并且不使用Collections.sort

答案 1 :(得分:0)

您需要一个可按您所需标准排序的比较器:

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

如果你正在使用Java 8或9,这可以简化为:

testFlow.sort(Comparator.comparing(TestFlow::getRowNumber));
相关问题