Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了数组 – 按字母顺序排列字符串数组大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我一直在学习使用Groovy中的数组.我想知道如何按字母顺序排列字符串数组.我的代码当前从用户获取字符串输入并按顺序打印出来:

@H_403_11@system.in.withReader { def country = [] print 'Enter your ten favorite countries:' for (i in 0..9) country << it.readLine() print 'Your ten favorite countries in order/n' println country //prints the array out in the order in which it was entered print 'Your ten favorite countries in reverse' country.reverseEach { println it } //reverses the order of the array

我如何按字母顺序打印出来?

解决方法

sort()是你的朋友.

country.sort()将按照字母顺序对突变国家进行排序.
country.sort(false)将按字母顺序对国家排序,返回排序列表.

@H_403_11@def country = ['Ireland','Iceland','Hungary','Thailand'] assert country.sort() == ['Hungary','Ireland','Thailand'] assert country == ['Hungary','Thailand'] country = ['Ireland','Thailand'] assert country.sort(false) == ['Hungary','Thailand'] assert country == ['Ireland','Thailand']

大佬总结

以上是大佬教程为你收集整理的数组 – 按字母顺序排列字符串数组全部内容,希望文章能够帮你解决数组 – 按字母顺序排列字符串数组所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。