1

How to sort the Version(i.e) '1.4.7','5.19.0','5.18.0','5.14.1','5.8.1','5.8.2','5.20.1','5.20.0' something like this. Initially tried with splitting by DOT and then comparing the First value in a list and second and so on, which worked totally fine or using bubble sort.

But then Requirement changed and sorting Input is AlphaNumneric

{'1.4.7','5.14.2', '8.2.alpha', '9.beta', 'other'}

Which I changed to an integer by using string CharAt. But it fails at some test cases as 5.14.1 value is around 100+ and 5.20.1 value is less than 100 and it's wrong.

b = b + integer.valueOf(val.charAt(k));

Saw Apex CompareTo Not sure if it will work in my case or No idea How it works.

Will it works for my requirement and please provide the sample code.

1

I recommend create separate class for version which implements Comparable interface

global class VersionNumber implements Comparable {
    private String versionName;
    private String major;
    private String minor;
    private String build;

    public VersionNumber(String versionName) {
        this.versionName = versionName;
        parse();
    }

    private void parse() {
        List<String> tokens = this.versionName.split('\\.');
        System.debug(tokens);
        this.major = tokens[0];
        this.minor = tokens.size() >= 2 ? tokens[1] : '';
        this.build = tokens.size() > 2 ? tokens[2] : '';
    }

    // Implement the compareTo() method
    global Integer compareTo(Object compareTo) {
        VersionNumber compareToVersionNumber = (VersionNumber)compareTo;
        if (this.major == compareToVersionNumber.major
                && this.minor == compareToVersionNumber.minor
                && this.build == compareToVersionNumber.build) {
            return 0;
        }
        Integer compResult = compare(this.major, compareToVersionNumber.major);
        if (compResult != 0) {
            return compResult;
        }
        compResult = compare(this.minor, compareToVersionNumber.minor);
        if (compResult != 0) {
            return compResult;
        }
        return compare(this.build, compareToVersionNumber.build);
    }

    private Integer compare(Object a, Object b) {
        if (a == null) {
            return -1;
        }
        if(b == null){
            return 1;
        }
        if (a instanceof Integer) {
            if (b instanceof Integer) {
                Integer intA = Integer.valueOf(a);
                Integer intB = Integer.valueOf(b);
                Integer aSubB = intA - intB;
                return aSubB > 0 ? 1 : (aSubB == 0 ? 0 : -1);
            }
            return -1; // Integer < String
        }
        if (b instanceof String) {
            return ((String) a).compareTo((String) b);
        }
        return 1; // String > Integer
    }
}

Testing:

List<VersionNumber> versions = new List<VersionNumber> {
        new VersionNumber('other'),
        new VersionNumber('1.4.7'),
        new VersionNumber('9.beta'),
        new VersionNumber('5.14.2'),
        new VersionNumber('8.2.alpha')
};
versions.sort();
System.debug(versions);
-1

Below sort will work for array consisting of alphanumberic numbers. You can use it as a way to implement sort for including dots.

var points = [40, 100, "2rt", "other", "asd", "12wer", 1, 5, 25, 10,];

    points.sort(function (a, b) {
        if (((typeof a) === 'number') && ((typeof b) === 'number'))
            return (a - b);
        else if (((typeof a) === 'number') && ((typeof b) !== 'number'))
            return -1;
        else if (((typeof a) !== 'number') && ((typeof b) === 'number'))
            return 1;
        else {
            if (parseInt(a) && parseInt(b)) return (parseInt(a) - parseInt(b));
            else if (parseInt(a) && !parseInt(b)) return -1;
            else if (!parseInt(a) && parseInt(b)) return 1;
            return -1;
        }
    });
  • What would this look like (roughly) in Apex? – Keith C Aug 1 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.