Can Project Versions be sorted alphabetically?

Daniel Bower January 21, 2016

In the Project Versions list, when adding a new version it's inserted at the top of the list.  It seems the only way to reorder the list is by drag/drop.  This ability exists for sorting customer field options, but not in the Project Versions screen. 

 

Is there a built in method (preferable) or an add-on (acceptable) that can sort the Project Versions list alphabetically?  Currently running 6.3.12

2 answers

1 accepted

1 vote
Answer accepted
Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2016

I've thrown this together; a JIRA admin can run it using the Script Console under "Add-Ons":

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.project.version.VersionManager;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.project.Project;
 
VersionManager versionManager = ComponentManager.getInstance().getVersionManager();
ProjectManager projectManager = ComponentManager.getInstance().getProjectManager();


Project project = projectManager.getProjectByCurrentKey("MYPROJECT");
List<Version> versions = versionManager.getVersions(project);


if (versions != null && versions.size() > 0) {
    Collections.sort(versions, new Comparator<Version>(){
      public int compare(Version obj1, Version obj2) {
        if (obj1 == null) {
            return -1;
        }
        if (obj2 == null) {
            return 1;
        }
        if (obj1.getName() == obj2.getName()) {
            return 0;
        }
        return obj2.getName().compareTo(obj1.getName());
      }
    });
    
    versionManager.moveToStartVersionSequence(versions[0]);
    for(int i=1;i<versions.size(); i++) {
        versionManager.moveVersionAfter(versionManager.getVersion(versions[i].getId()),versions[i-1].getId());
    }
}
return versions;

I had to use "getVersion()" in the call to "moveVersionAfter()", because, otherwise, the signature of the Version object in the List didn't always match the underlying collection due to having been re-sequenced by a previous call, and so the method failed.  Additionally, this is using the basic String.compareTo() comparison, which means "10" comes after "2"; with your version padding, that may not be an issue, but it's something I'll have to work on for my own in a bit.

If you uncomment "return versions" after the sort method, you can see what the sorted list will look like before running it with side effects enabled.  And if you want them sorted in ASC order instead of DESC order, switch the ob2/ob1 in the compareTo call.  And, of course, replace MYPROJECT with your project of interest.

Daniel Bower January 28, 2016

Jeremy, this is fantastic, thank you for putting this together. 

Matthew Frassetti May 2, 2017

Jeremy, I am currently trying to run this script in my own JIRA instance, but I am getting an error message. 

 

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 15: expecting ')', found ';' @ line 15, column 26.
   if (versions != null && versions.size() > 0) {
                            ^

1 error

 at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:26)
 at ScriptExecution1_groovyProxy.run(Unknown Source)




Any idea why this could be? Current JIRA version is 7.2.7

 

Screen Shot 2017-05-02 at 5.01.32 PM.png

Donald Skanes December 14, 2017

I am having the same issue. Is there an update for this?

Volantsoft March 17, 2019

Replace codes with real double amps.

1 vote
Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2016

I don't think there's a built-in way, though I think I could write an ad-hoc Script Runner script to do this fairly easily, and I actually have a use for that.  Do you want to sort numerically or alphanumerically?  I think the typical use case is by release date, which is why new versions are added to the top.

Daniel Bower January 21, 2016

Thanks, I'd be looking for alpha numeric sort.  Our versions are generally numeric (01.02.03), but some have alpha characters (01.02.Beta).

Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2016

I ask because, in an alpha-numeric sort, 02 comes before 1; in a numeric sort, 1 comes before 02, or such is my interpretation.  I suppose I could be making up the terminology; the unix "sort" utility also has "general numeric sort", which I'm not sure I understand, unless that's what I'm calling "alpha-numeric".

Jeremy Gaudet
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 21, 2016

As long as everything is padded consistently, alpha-numeric sorting should work.  However, I think "Beta" would come after any numeric values.  Do you want non-numeric fields to come before numeric fields?  It seems like you'd want "Beta", then "01", etc.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events