groovy排序按字母顺序排列(groovy sort alphabetically)

编程入门 行业动态 更新时间:2024-10-25 08:19:28
groovy排序按字母顺序排列(groovy sort alphabetically)

我正在更新一个groovy脚本,在SoapUI中构建一个测试运行器套件。 脚本扫描文件夹并为每个文件夹中的每个文件添加测试用例。

我试图解决的问题是脚本使用的是eachFileMatch() ,它在Windows和Unix文件系统之间没有一致的行为。 我需要更新脚本,以便按字母顺序列出文件。

我对groovy很新,所以我不知道从哪里开始。 看到sort()方法确实存在,但我不确定我是否可以在eachFileMatch上使用它。

这是我需要调整的代码片段:

new File(projectPathTest+"/nord").eachDir{dir-> log.info("Dir > "+dir); operation = dir.name def wsdlTestCase = testSuite.addNewTestCase( operation ) wsdlTestCase.setFailOnError(false) dir.eachFileMatch(~/.*_request\.xml/){file-> // Need Alphabetically sorting here log.info("File >> "+file) addTestStep(operation, file, wsdlTestCase, projectPath, endPoint) }

这样做的任何出发点都会非常感谢groovy方法,因为现在我没有时间深入研究groovy API。

问候 }

I am updating a groovy script which build a test runner suite inside SoapUI. The scripts scan a folders and add test case for each file in each folders.

The problem I am trying to solve is that the script is using eachFileMatch() which does not have a consistent behavior between Windows and Unix file systems. I need to update the script so that files are listed alphabetically.

I am quite new to groovy so I don't know where to get started. Saw that a sort() method does exist but I am not sure I can use it on eachFileMatch.

Here's the code snippet I need to adapt :

new File(projectPathTest+"/nord").eachDir{dir-> log.info("Dir > "+dir); operation = dir.name def wsdlTestCase = testSuite.addNewTestCase( operation ) wsdlTestCase.setFailOnError(false) dir.eachFileMatch(~/.*_request\.xml/){file-> // Need Alphabetically sorting here log.info("File >> "+file) addTestStep(operation, file, wsdlTestCase, projectPath, endPoint) }

Any starting point to do it will the groovy approach would be really appreciated as for now i don't have time to delve into the groovy API.

Regards }

最满意答案

您可以在TreeSet收集文件,这些文件将维护其排序顺序,然后遍历集合以显示或以其他方式处理您的文件集合。

TreeSet<File> files = new TreeSet<File>() dir.eachFileMatch(~/.*_request\.xml/){file-> files << file } files.each { f-> // log or do whatever with the files in order }

如果您需要对所有文件进行此排序,则需要将TreeSet放在eachDir闭包之外。

You can collect the files in a TreeSet which will maintain their sort order and then iterate over the set to display or otherwise act on your collection of files.

TreeSet<File> files = new TreeSet<File>() dir.eachFileMatch(~/.*_request\.xml/){file-> files << file } files.each { f-> // log or do whatever with the files in order }

If you need this ordering over all the files, you will need to put the TreeSet outside of the eachDir closure.

更多推荐

本文发布于:2023-07-16 22:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1135393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字母   顺序排列   groovy   sort   alphabetically

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!