根据子字符串对Java中的字符串进行排序

根据子字符串对Java中的字符串进行排序

本文介绍了根据子字符串对Java中的字符串进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串列表, def123,abc999,zzz000,abc123,zzz111 .我希望对列表进行排序,以使前三个字符以升序排序,而后三个字符以降序排序.因此输出应为 abc999,abc123,def123,zzz111,zzz000 这可能吗?

I have a list of strings def123, abc999, zzz000, abc123, zzz111.I want the list sorted such that first three characters are sorted in ascendng order and next three in descending.So the output should be abc999, abc123, def123, zzz111,zzz000Is this possible?

推荐答案

其他答案建议您实现 Comparator .对于Java 8接口中添加的最新实用程序方法,这不再是必需的:

Other answers have suggested you implement Comparator. That's no longer necessary with recent utility methods added to the interface in Java 8:

list.sort(Comparator
    .comparing(s -> s.substring(0, 3))
    .thenComparing(s -> s.subtring(3, 6), Comparator.reverseOrder()));

还要注意, List 现在有一个 sort 方法.

Also note that List now has a sort method.

这篇关于根据子字符串对Java中的字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:58