gpt4 book ai didi

java - 对数组列表进行排序和分类

转载 作者:行者123 更新时间:2023-12-01 17:31:33 26 4
gpt4 key购买 nike

我想根据字段“代码”对 Arraylist 进行排序,如下所示,并且我想在当前数组列表中添加一条附加记录,其值按相同代码的总价格计算。

No      Desc    Width      Height       Code        Price

1 WW 88.0 88.0 1021021 340.0
4 TT 55.0 55.0 1021021 340.0
5 PP 66.0 66.0 1021021 340.0
2 gg 66.0 66.0 1021022 320.0
3 LL 658.0 652.0 1021022 320.0

我的预期输出是这样的

1        WW     88.0        88.0      1021021       340.0       
4 TT 55.0 55.0 1021021 340.0
5 PP 66.0 66.0 1021021 340.0
1020
2 gg 66.0 66.0 1021022 320.0
3 LL 658.0 652.0 1021022 320.0
640

谁能提出解决这个问题的最佳方法?提前致谢。

最佳答案

如果你使用java,你可以自己写Comparator并将 ArrayList 和比较器传递给 Collections.sort() method .

来自 Javadoc:

A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering.

关于java - 对数组列表进行排序和分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10345870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com