Groovy web console

subscribe to the feed Subscribe
to this
site

SortbByAlphaNumeric&Numeric

Published 1 month ago by Ramkumar with tags alphanumeric sort
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
static def getSort(doc, res) {
    def parentname = doc['name'];
    def displayName = parentname;
    def text = 'not'
    double mc = doc['name'].toLowerCase().indexOf(text);
    if (mc == -1) {
        for (an in doc['acc_names']) {
            def c = an.toLowerCase().indexOf(text);
            if (c > -1 && (mc == -1 || c < mc)) {
                def pname = ' (managed by ' + parentname + ')'
                mc = c;
                if (c == 0) {
                    displayName = text + '...' + pname;
                } else if ((text.length() + c) == an.length()) {
                    displayName = '...' + text + pname;
                } else {
                    displayName = '...' + text + '...' + pname;
                }
            }
        }
        mc += 0.1
    }
    res.add mc+'~'+displayName
}
def res = [];
def docs = [
        ["name": "anot", "acc_names": ["hhhh 24/7 testing", "hh fffff 24/7 testing"]],
        ["name": "aanot", "acc_names": ["hhhh 24/7 testing", "hh fffff 24/7 testing"]],
        ["name": "aaaaaaaaaaaaanot", "acc_names": ["hhhh 24/7 testing", "hh fffff 24/7 testing"]],
        ["name": "aaanot", "acc_names": ["hhhh 24/7 testing", "hh fffff 24/7 testing"]]

]
docs.each {getSort(it, res)}

println res

println res.sort()