如何在Groovy中根据数字按字母数字中定义的字段对类列表进行排序?

goucqfw6  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(144)

我需要检查具体排序。有类列表:

[
no = '1'
field = 'FEE_HIG_R_test1234_2',

no = '2'
field = 'FEE_HIG_R_test1234_11',

no = '3'
field = 'FEE_HIG_R_test1234_10',

no = '4'
field = 'FEE_HIG_R_test1234_1',

no = '5'
field = '06633-146944-0000036012',

no = '6'
field = '06633-155867-0000051910',

no = '7'
field = '06687-250844-00002544203'
]

我必须根据数字按字母数字字段进行排序,并得到如下结果:

[
no = '5'
field = '06633-146944-0000036012',

no = '6'
field = '06633-155867-0000051910',

no = '7'
field = '06687-250844-00002544203',

no = '4'
field = 'FEE_HIG_R_test1234_1',

no = '1'
field = 'FEE_HIG_R_test1234_2',

no = '3'
field = 'FEE_HIG_R_test1234_10',

no = '2'
field = 'FEE_HIG_R_test1234_11'
]

我尝试了:result.sort{ a,B -〉(((a.field =~ /\d+/)[-1] as Integer)〈=〉((b.field =~ /\d+/)[-1] as Integer)))}
但结果是

[
no = '4'
field = 'FEE_HIG_R_test1234_1',

no = '1'
field = 'FEE_HIG_R_test1234_2',

no = '3'
field = 'FEE_HIG_R_test1234_10',

no = '2'
field = 'FEE_HIG_R_test1234_11',

no = '5'
field = '06633-146944-0000036012',

no = '6'
field = '06633-155867-0000051910',

no = '7'
field = '06687-250844-00002544203'
]

有人能帮忙吗?

nbnkbykc

nbnkbykc1#

类似这样的一行代码:

def data = [
  'FEE_HIG_R_test1234_2',
  '06687-250844-00002544204',
  '06687-250844-00002544203',
  '06687-250844-00002544202',
  'FEE_HIG_R_test1234_1',
  '06633-146944-0000036012',
  '06633-155867-0000051910',
  'FEE_HIG_R_test1234_11',
  'FEE_HIG_R_test1234_10',
]

def result = data.sort{ 
   it ==~ /\d+-\d+-\d+/ 
      ? it.replaceAll( '-', '' ).toBigDecimal() 
      : 10000000000000000000000 + ( it =~ /\d+/ )[ -1 ].toInteger() 
}

assert result.join( '\n' ) == '''\
06633-146944-0000036012
06633-155867-0000051910
06687-250844-00002544202
06687-250844-00002544203
06687-250844-00002544204
FEE_HIG_R_test1234_1
FEE_HIG_R_test1234_2
FEE_HIG_R_test1234_10
FEE_HIG_R_test1234_11'''

需要1000000... +将字符串中较小的数字(如FEE_HIG_R_test1234_1)进一步向右推。

相关问题