Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting Groovy排序字符串';ArrayList的s_Sorting_Groovy - Fatal编程技术网

Sorting Groovy排序字符串';ArrayList的s

Sorting Groovy排序字符串';ArrayList的s,sorting,groovy,Sorting,Groovy,我尝试从ArrayList中按顺序对字符串排序。我有点不知道怎么做。例如,我有我的订单列表: def my_order = ['Version', 'Author', 'Somethink'] def files_sort = pack.files // here have string's to files 在文件中_sort具有随机字符串,如db/author/test.sql、db/Somethink/test1.sql以及许多其他字符串。 我认为最简单的方法是在我的文件上使用循环,并根

我尝试从ArrayList中按顺序对字符串排序。我有点不知道怎么做。例如,我有我的订单列表:

def my_order = ['Version', 'Author', 'Somethink']
def files_sort = pack.files // here have string's to files
文件中_sort
具有随机字符串,如
db/author/test.sql
db/Somethink/test1.sql
以及许多其他字符串。 我认为最简单的方法是在我的文件上使用循环,并根据我的订单列表进行排序?例如:

files.sort.each {
   it.sort(here give my_order ??)
}
谢谢你的提示

编辑:

输入列表:

def files_to_sort = ['db/author/test1.sql', 'db/author/foo.sql', 'db/version/test1.sql', 'db/Somethink/foo.sql']
my_顺序排序的输出列表
list:

def files_after_sort = ['db/version/test1.sql', 'db/author/test1.sql', 'db/author/foo.sql', 'db/Somethink/foo.sql']

所以为了得到排序的数组,我循环了
my_order
,并在其中再次循环了文件

my_order.each{ type ->
        files.sort.each{ file ->
            if(file =~ /.*$type.*/){
                println file
            }
        }
}
def files=['db/author/test1.sql','db/author/foo.sql','db/version/test1.sql','db/Somethink/foo.sql']
//将数组转换为大写值以支持忽略大小写
def my_order=['Version','Author','Somethink'].collect{it.toUpperCase()}
//用于计算字符串'x'的排序索引的闭包
def my_index={x->
x=x.toUpperCase()
返回我的_order.findIndexOf{x.contains(it)}
}
//排序比较:首先按索引进行比较,如果索引相同,则按值本身进行比较
排序{a,b->my_index(a)my_index(b)?:ab}
println文件

什么是输入和期望的输出?输入是字符串,看起来像:
db/author/test.sql
但是
author
是动态的,在文件中\u排序以随机顺序获取文件。在输出中,我想要相同的字符串,但顺序由
my_order
给出,但如何查找特定字符串是版本、作者还是其他内容?在一个字符串中,只出现我在
my_order
中的一个单词。每次字符串都会有相同的构建外观。这不是很安全:如果你有一个名为
db/author/version1.sql的文件,我想你会打印两次。
def files = ['db/author/test1.sql', 'db/author/foo.sql', 'db/version/test1.sql', 'db/Somethink/foo.sql']
//convert array to uppercase values to support ignore-case
def my_order = ['Version', 'Author', 'Somethink'].collect{it.toUpperCase()} 

//closure to calculate sorting index of string `x` 
def my_index = {x->
    x = x.toUpperCase()
    return my_order.findIndexOf{ x.contains(it) }
}
//the compare in sort: first - by index, if index is same then by value itself
files.sort{a,b->  my_index(a)<=>my_index(b) ?: a<=>b }
println files