private static void addOrderPossiblyNested(CriteriaQuery query,
                                               From queryRoot,
                                               CriteriaBuilder criteriaBuilder,
                                               PersistentEntity entity,
                                               String sort,
                                               String order,
                                               boolean ignoreCase) {
        int firstDotPos = sort.indexOf(".");
        if (firstDotPos == -1) {
            addOrder(entity, query, queryRoot, criteriaBuilder, sort, order, ignoreCase);
        } else { // nested property
            String sortHead = sort.substring(0, firstDotPos);
            String sortTail = sort.substring(firstDotPos + 1);
            PersistentProperty property = entity.getPropertyByName(sortHead);
            if (property instanceof Embedded) {
                // embedded objects cannot reference entities (at time of writing), so no more recursion needed
                addOrder(entity, query, queryRoot, criteriaBuilder, sort, order, ignoreCase);
            } else if (property instanceof Association) {
                Association a = (Association) property;
                final Join join = queryRoot.join(sortHead);
                PersistentEntity associatedEntity = a.getAssociatedEntity();
                Class<?> propertyTargetClass = associatedEntity.getJavaClass();
                addOrderPossiblyNested(query, join, criteriaBuilder, associatedEntity, sortTail, order, ignoreCase); // Recurse on nested sort
            }
        }
    }
private static void addOrderPossiblyNested ( CriteriaQuery query , From queryRoot , CriteriaBuilder criteriaBuilder , PersistentEntity entity , String sort , String order , boolean ignoreCase ) { int firstDotPos = sort . indexOf ( "." ) ; if ( firstDotPos == - 1 ) { addOrder ( entity , query , queryRoot , criteriaBuilder , sort , order , ignoreCase ) ; } else { String sortHead = sort . substring ( 0 , firstDotPos ) ; String sortTail = sort . substring ( firstDotPos + 1 ) ; PersistentProperty property = entity . getPropertyByName ( sortHead ) ; if ( property instanceof Embedded ) { addOrder ( entity , query , queryRoot , criteriaBuilder , sort , order , ignoreCase ) ; } else if ( property instanceof Association ) { Association a = ( Association ) property ; final Join join = queryRoot . join ( sortHead ) ; PersistentEntity associatedEntity = a . getAssociatedEntity ( ) ; Class < ? > propertyTargetClass = associatedEntity . getJavaClass ( ) ; addOrderPossiblyNested ( query , join , criteriaBuilder , associatedEntity , sortTail , order , ignoreCase ) ; } } }
Add order to criteria, creating necessary subCriteria if nested sort property (ie. sort:'nested.property').