Skip to content

Instantly share code, notes, and snippets.

@dmlloyd
Last active July 24, 2020 16:53
Show Gist options
  • Select an option

  • Save dmlloyd/dd35d41dccaf8e9d1fb22a99e25c15bb to your computer and use it in GitHub Desktop.

Select an option

Save dmlloyd/dd35d41dccaf8e9d1fb22a99e25c15bb to your computer and use it in GitHub Desktop.
Sort maven versions neatly
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.apache.maven:maven-artifact:3.6.3
import org.apache.maven.artifact.versioning.ComparableVersion;
import java.util.*;
public class ComprehensiveSort {
static final Comparator<String> c = (a, b) -> Integer.signum(new ComparableVersion(a).compareTo(new ComparableVersion(b)));
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
for (char c : new char[] { '0', '1', 'x' }) {
for (char d : new char[] { '0', '1', 'x' }) {
for (char e : new char[] { '0', '1', 'x' }) {
list.add("" + c + "." + d + "." + e);
//list.add("" + c + "." + d + "." + e + "-1");
list.add("" + c + "." + d + "." + e + "-x");
}
}
}
Collections.sort(list, c);
for (String s : list) {
System.out.println(s);
}
}
}
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.apache.maven:maven-artifact:3.6.3
import org.apache.maven.artifact.versioning.ComparableVersion;
import java.util.*;
public class VersionSort {
static final Comparator<String> c = (a, b) -> Integer.signum(new ComparableVersion(a).compareTo(new ComparableVersion(b)));
public static void main(String[] args) {
Collections.sort(Arrays.asList(args), c);
for (String s : args) {
System.out.println(s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment