• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC Design
  3. Sorting list of version numbers

Stats

  • Locked Locked
  • Replies 4
  • Subscribers 125
  • Views 6462
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Sorting list of version numbers

MorrisDH
MorrisDH over 2 years ago

Have a list of version numbers. Example list("v-0.1" "v-1.0" "v-1.1" "v-2.0" "v-11.0" "v-11.2" "v-11.10")

The example is a sorted list and the latest is "v-11.10". The lastest is determined primarily by the number before the dot and secondarily by the number after the dot.

Suppose this list was not sorted? For example, given list("v-1.1" "v-1.0" "v-0.1" "v-11.10" "v-11.0" "v-11.2" "v-2.0") how can I sort this to get the version numbers in order?

  • Cancel
Parents
  • Andrew Beckett
    Andrew Beckett over 2 years ago

    You can use this function:

    ; put in a file with a .ils suffix since this uses local functions
    procedure(CCFsortVersions(versionList)
      let(((versionPat pcreCompile("^v-(\\d+)\\.(\\d+)")))
        procedure(parseVersion(versionNum)
          letseq((
            (match pcreExecute(versionPat versionNum))
            (major match && atoi(pcreSubstitute(versionPat "\\1")) || 0)
            (minor match && atoi(pcreSubstitute(versionPat "\\2")) || 0)
            )
            list(major minor)
          ))
        procedure(compare(versionA versionB)
          destructuringBind((majorA minorA) parseVersion(versionA)
            destructuringBind((majorB minorB) parseVersion(versionB)
              if(majorA==majorB then minorA<minorB else majorA<majorB)
            )))
        sort(versionList compare)
      )
    )

    This uses regular expressions to parse the version numbers, and then compares using the major versions, falling back to the minor numbers as a tie-breaker.

    Regards,

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • Andrew Beckett
    Andrew Beckett over 2 years ago

    You can use this function:

    ; put in a file with a .ils suffix since this uses local functions
    procedure(CCFsortVersions(versionList)
      let(((versionPat pcreCompile("^v-(\\d+)\\.(\\d+)")))
        procedure(parseVersion(versionNum)
          letseq((
            (match pcreExecute(versionPat versionNum))
            (major match && atoi(pcreSubstitute(versionPat "\\1")) || 0)
            (minor match && atoi(pcreSubstitute(versionPat "\\2")) || 0)
            )
            list(major minor)
          ))
        procedure(compare(versionA versionB)
          destructuringBind((majorA minorA) parseVersion(versionA)
            destructuringBind((majorB minorB) parseVersion(versionB)
              if(majorA==majorB then minorA<minorB else majorA<majorB)
            )))
        sort(versionList compare)
      )
    )

    This uses regular expressions to parse the version numbers, and then compares using the major versions, falling back to the minor numbers as a tie-breaker.

    Regards,

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
  • MorrisDH
    MorrisDH over 2 years ago in reply to Andrew Beckett

    Works like a charm.  That code is way over my head. But that's OK for now.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • MorrisDH
    MorrisDH over 2 years ago in reply to Andrew Beckett

    I found cases where the "string" portion of the version list (the 'v' in my example) can vary.

    Changed the script to take the string part as an additional argument, CCFsortVersions(versionList model). Then changed the pcreCompile to pcreCompile(strcat("^" model "-(\\d+)\\.(\\d+)"))

    It seems to work.

    Then I found another curve ball in some version lists. Sometimes the string can vary within a list. In this case it's anyone's guess as to which version should be 'latest'. The function still runs and if I change the string argument the output can change.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 2 years ago in reply to MorrisDH

    You could make the code like this:

    procedure(CCFsortVersions(versionList)
      let(((versionPat pcreCompile("^(v|V|ver)-(\\d+)\\.(\\d+)")))
        procedure(parseVersion(versionNum)
          letseq((
            (match pcreExecute(versionPat versionNum))
            (major match && atoi(pcreSubstitute(versionPat "\\2")) || 0)
            (minor match && atoi(pcreSubstitute(versionPat "\\3")) || 0)
            )
            list(major minor)
          ))
        procedure(compare(versionA versionB)
          destructuringBind((majorA minorA) parseVersion(versionA)
            destructuringBind((majorB minorB) parseVersion(versionB)
              if(majorA==majorB then minorA<minorB else majorA<majorB)
            )))
        sort(versionList compare)
      )
    )

    Note that I added a group of alternatives (v|V|ver) in the pattern, and because there was another grouping I incremented the numbers in the pcreSubstitute lines. That would cope with v-1.2, V-2.3, ver-5.60 etc.

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information