Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:24:37+00:00 2026-05-26T04:24:37+00:00

Hey I have created a Groovy script that will extract the version numbers of

  • 0

Hey I have created a Groovy script that will extract the version numbers of some folder. I would then like to compare the version numbers and select the highest.

I got my script to run through the dir folder and I then get the versions in this format: 02.2.02.01

So I could get something like this:

  • 02.2.02.01
  • 02.2.02.02
  • 02.2.03.01

I don’t have them as a list but like this:

baseDir.listFiles().each { file -> 
  def string = file.getName().substring(5, 15)
  // do stuff
}

Also I have tested that Groovy could compare them with the > operator and it can! But now I need to select the one with the highest version

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:24:37+00:00Added an answer on May 26, 2026 at 4:24 am

    This appears to work

    String mostRecentVersion(List versions) {
      def sorted = versions.sort(false) { a, b -> 
    
        List verA = a.tokenize('.')
        List verB = b.tokenize('.')
         
        def commonIndices = Math.min(verA.size(), verB.size())
        
        for (int i = 0; i < commonIndices; ++i) {
          def numA = verA[i].toInteger()
          def numB = verB[i].toInteger()
          
          if (numA != numB) {
            return numA <=> numB
          }
        }
        
        // If we got this far then all the common indices are identical, so whichever version is longer must be more recent
        verA.size() <=> verB.size()
      }
      
      println "sorted versions: $sorted"
      sorted[-1]
    }
    

    Here is an inadequate set of tests. You should add some more.

    assert mostRecentVersion(['02.2.02.01', '02.2.02.02', '02.2.03.01']) == '02.2.03.01' 
    assert mostRecentVersion(['4', '2']) == '4'
    assert mostRecentVersion(['4.1', '4']) == '4.1'
    assert mostRecentVersion(['4.1', '5']) == '5'
    

    Run this code and the tests in the Groovy console to verify that it works

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all i have created dynamic buttons at runtime and i would like to
Hey i have this div that shows as a popup: <div class=popup> </div> Then
Hey guys, I have an application that I want to display some data from
Hey all,(im a beginner in rails) i've created a controller that look like that:
Hey guys. Basically I have a CSS triangle that I've created, and it sits
Hey, I have an iphone program that has a parser that parses some XML
Hey, sorry for my bad english... Using EF4 code-only, I have created some POCO
Hey Guys, i have created the following Menu Structure: <div id=menu> <ul> <li><a href=#>Main
Hey, so I have created a function to check the DB for unique entries,
Hey I have a really annoying IE7 bug that I am trying to work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.