【问题标题】:Change order of output from java List更改 java List 的输出顺序
【发布时间】:2018-12-03 09:49:25
【问题描述】:

我从数据库中得到一个包含每月数据的列表。目前我已经根据数据库将数据从January 排序到December

但我希望输出应该从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;
}

【问题讨论】:

  • 是否可以更改数据库查询或者这不是一个选项?
  • 到目前为止你尝试过什么?了解比较器
  • 不幸的是,我不需要前端中的列表完全相同,但也如上所述。不想使用第二个查询
  • 我们甚至不知道您如何获取这些数据,或者您如何打印它。所以,请阅读minimal reproducible example 并向我们展示您解决此问题的尝试,并指出您遇到的具体问题。
  • 如果逻辑说一年开始,例如,六月,我想操纵列表,以便输出从六月开始到五月,而不是从一月到十二月。我想用Java解决这个问题。数据作为 Json 发送到前端。我们使用 Angular 作为我们的前端。但我只做后端部分

标签: java list sorting output


【解决方案1】:

如果可以修改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

【讨论】:

  • 数据库用于存储您的数据。您用来向用户显示它的 order 不是数据库应该规定的。换句话说:根据用户要求,可能有 10 种不同的方式来对查询结果进行排序。因此,当您需要超越简单事物的自定义订单时,您应该将相应的排序放入您的业务逻辑中。
  • 完美地说,这取决于情况。这是许多可能的方法之一。
  • 我首先想说“SQL 中的那些废话”,但我认为了解所有选项真的很有意义。如果您想从数据库中获取 1000 万个条目,并且您需要一种特定的排序,当然,让数据库为您完成这项工作。因此,如果您添加诸如“或在源代码中执行,取决于您的要求”之类的内容,我的支持将与您同在 ;-)
猜你喜欢
  • 2013-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
  • 2020-10-03
  • 2012-08-31
  • 1970-01-01
相关资源
最近更新 更多