![](/style/images/good.png)
3
![](/style/images/bad.png)
像这种代码如何优化 根据字段排序
source link: https://www.v2ex.com/t/790656
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
不能直接在数据库排序 数据要从数据库查出组装,然后排序
5 条回复 • 2021-07-20 20:27:42 +08:00
potatowish 4 小时 15 分钟前 via iPhone
维护一个枚举类型,column,sortType,Comparator,isReversed, 再写一个方法根据 column sortType isReversed 匹配到唯一的枚举,另外一个方法根据枚举来动态处理数据
ForkNMB 3 小时 18 分钟前
逆序不要用 reversed 噢,没啥必要,直接在比较器里传参就行,list.stream().sorted(Comparator.comparing(类::属性一,Comparator.reverseOrder()));
至于优化的话,维护一个枚举类,维护不同种类的比较器即可(比较器的字段类型,是否逆序这些信息)
至于优化的话,维护一个枚举类,维护不同种类的比较器即可(比较器的字段类型,是否逆序这些信息)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK