Can Project Versions be sorted alphabetically?

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

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.

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

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

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

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.

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).

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".

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 Join to answer
Community showcase
Emilee Spencer
Published yesterday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

37 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot