我有一个这样的字符串列表:-
listOf("abc", "a", "bb", "aa", "aaa", "bb", "a")
我想要这样的输出:-
listOf("a", "a", "aa", "bb", "bb", "aaa", "abc")
首先,我想按长度对列表进行排序,然后再次按字母对长度组进行排序。
到目前为止,我尝试使用以下代码
fun main() {
val result = listOf("abc", "a", "bb", "aa", "aaa", "bb", "a").groupBy { it.length }
val valueList = ArrayList(result.values).flatMap { it.toList() }
println(valueList)
}
但是我得到的结果如下
[abc, aaa, a, a, bb, aa, bb]
@Sergey Lagutin的重复评论后 我也尝试过
val sortedList = a.sortedWith(compareBy { it.length })
没有返回期望的结果
答案 0 :(得分:2)
val a = listOf("abc", "a", "bb", "aa", "aaa", "bb", "a")
a.sortedWith(compareBy({ it.length }, { it })) // [a, a, aa, bb, bb, aaa, abc]
答案 1 :(得分:1)
您可以尝试这种方式
val yourList = listOf("abc", "a", "bb", "aa", "aaa", "bb", "a")
val yourSortedList = yourList.sorted().sortedBy { it.length }
结果[a, a, aa, bb, bb, aaa, abc]
答案 2 :(得分:0)
在集合上使用class SaveEveryEpoch(Callback):
def __init__(self, model_name, *args, **kwargs):
self.model_checkpoints_with_loss = []
self.model_name = model_name
super().__init__(*args, **kwargs)
def on_epoch_end(self, epoch, logs):
# I suppose here it's a Functional model
print(logs['acc'])
path_to_checkpoint = (
str(datetime.datetime.now()).split(' ')[0]
+ f'_{self.model_name}'
+ f'_{epoch:02d}.hdf5'
)
self.model.save(path_to_checkpoint)
self.model_checkpoints_with_loss.append((logs['loss'], path_to_checkpoint))
函数
sortedWith