Search code examples
orientdb

ORIENTDB customize Order By in sql query


Well, I got these indexes in this format like this 10.9.8.1 in a string.

I want to order records with SQL query in order by most significant int left.

like:

1

1.3

1.3.1

1.3.4

1.3.5

7.8

10.4.5

10.5

11.3

How this can be achieved?


Solution

  • You could use a javascrip function with the parameter "yourProperty" like this:

    var g=orient.getGraph();
    var result=[];
    var list=[];
    var b=g.command("sql","select from v");
    for(i=0;i<b.length;i++){
            var record=b[i];
            var index=record.getProperty(yourProperty);
            index+="."+record.getId().toString();
            var vector=index.split('.');
            list.push(vector);
    }
    
    list.sort(function (a, b) {
        var dim1=a.length-1;
        var dim2=b.length-1;
        for(j=0;j<dim1;j++){  
            var value1=parseInt(a[j]);
            if(j<dim2){   
                var value2=parseInt(b[j]);
                if(value1>value2)
                    return 1;
                else if (value1<value2)
                    return -1;
            }
        }
        if(dim1>dim2)
            return 1;
        else
            return -1;
    });
    
    for(m=0;m<list.length;m++){ 
      var element=list[m];
      var id=element[element.length-1];
      for(h=0;h<b.length;h++){
        var id2=b[h].getId().toString();
            if(id==id2){
                result.push(b[h]);
                break;
            }
        }
    }
    
    return result;
    

    enter image description here

    Hope it helps.