从Java列表更改输出顺序

时间:2018-12-03 09:49:25

标签: java list sorting output

我从数据库中获得包含每月数据的列表。目前,我已经按照数据库将数据从JanuaryDecember进行了排序。

但是我希望输出应该从June开始,结束月份应该是May

示例输出列表:

    [
   {
    "accountlinevaluesId": 8,
    "accountlineId": 3,
    "month": "januar",
    "accountNumber": 177,
    "name": "Befestigungen",
    "amount": 0
},
{
    "accountlinevaluesId": 9,
    "accountlineId": 3,
    "month": "januar",
    "accountNumber": 179,
    "name": "Andere Bauten",
    "amount": -625
}, {
    "accountlinevaluesId": 165,
    "accountlineId": 4,
    "month": "februar",
    "accountNumber": 179,
    "name": "Andere Bauten",
    "amount": -625
},
{
    "accountlinevaluesId": 166,
    "accountlineId": 4,
    "month": "februar",
    "accountNumber": 210,
    "name": "Maschinen",
    "amount": -1962
}, {
    "accountlinevaluesId": 329,
    "accountlineId": 5,
    "month": "maerz",
    "accountNumber": 211,
    "name": "Testgestell",
    "amount": -897.75
},
{
    "accountlinevaluesId": 330,
    "accountlineId": 5,
    "month": "maerz",
    "accountNumber": 233,
    "name": "Motor f. Arbeitsplattfo",
    "amount": 0
}]

有人可以告诉我如何最好地进行以及如何用Java解决这个问题吗?

当前查询如下:

public List<AccountlinevaluesEntity> findAllAccountLineValuesByMandantId(Long mandantId, String title, String context, String month) {
    List<AccountlinevaluesEntity> accountLineValues = new ArrayList();
    if (mandantId > 0 && title != null && context !=null) {
        try {
            List accountIds = new AccountManager().findAccountByMandantId(mandantId);
            startOperation(false);
            if (accountIds != null && accountIds.size() > 0) {
                List accountLineIds =
                        getSession()
                                .createQuery("SELECT ale.accountlineId FROM AccountlineEntity ale WHERE ale.accountId IN :accountIds AND ale.title = :title AND ale.context = :context")
                                .setParameter("accountIds", accountIds)
                                .setParameter("title", title)
                                .setParameter("context", context)
                                .list();
                if (accountLineIds != null && accountLineIds.size() > 0) {
                    if (month == null) {
                        return getSession()
                                .createQuery("SELECT alve FROM AccountlinevaluesEntity alve WHERE alve.accountlineId IN :accountlineIds")
                                .setParameter("accountlineIds", accountLineIds)
                                .list();
                    } else {
                        return getSession()
                                .createQuery("SELECT alve FROM AccountlinevaluesEntity alve WHERE alve.accountlineId IN :accountlineIds AND alve.month = :month")
                                .setParameter("accountlineIds", accountLineIds)
                                .setParameter("month", month)
                                .list();
                    }
                }
            }
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            getSession().close();
        }
    }
    return accountLineValues;
}

1 个答案:

答案 0 :(得分:0)

如果您可以更改sql,则可以使用自定义排序逻辑来完成。

SELECT name, *
FROM accounts

ORDER BY
    (
     CASE
       WHEN (month == 'June') THEN 1
       WHEN (month == 'July') THEN 2

       .......
        WHEN (month == 'May') THEN 12
     END
    ),
    name