如何从Groovy中的组阵列中获取最小值和最大值

发布于 2025-01-25 17:32:28 字数 288 浏览 1 评论 0原文

我是Groovy的新手,我不知道从哪里开始这个问题。 我有一个XML文件,其中包含带有RID,ID和日期的多个条目。 我设法将元素放入一个数组中,我想提取每个RID分组的RID,最小日期和最大日期。如果小组中只有一个条目,最小日期和最大日期将相同。

Data:
1,1234,2022010101;
2,1235,2022020202;
2,1236,2022030303;

Preferred output:
1,2022010101,2022010101;
2,2022020202,2022030303;

I'm fairly new to Groovy and I don't know where to begin with this problem.
I have a xml file that contains multiple entries with rid, id and a date.
I've managed to get the elements into an array and I would like to extract rid, min date and max date grouped by each rid. If there is only one entry in the group, min and max date would be the same.

Data:
1,1234,2022010101;
2,1235,2022020202;
2,1236,2022030303;

Preferred output:
1,2022010101,2022010101;
2,2022020202,2022030303;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2025-02-01 17:32:28

尝试这样的事情:

def dataLines = [
    [1,1234,2022010101],
    [2,1235,2022020202],
    [2,1236,2022030303]
]

def output = dataLines.groupBy { it[0] }.collect { rid, lines ->
    def sorted = lines.sort{ it[2] }
    [ rid , sorted[0][2], sorted[-1][2] ]
}

output.each { println it }

Try something like this:

def dataLines = [
    [1,1234,2022010101],
    [2,1235,2022020202],
    [2,1236,2022030303]
]

def output = dataLines.groupBy { it[0] }.collect { rid, lines ->
    def sorted = lines.sort{ it[2] }
    [ rid , sorted[0][2], sorted[-1][2] ]
}

output.each { println it }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文