@ Override public < E > List < E > sort ( final List < E > elements ) { int position = elements . size ( ) ; do { int lastSwapPosition = 0 ; for ( int index = 1 ; index < position ; index ++ ) { if ( getOrderBy ( ) . compare ( elements . get ( index - 1 ) , elements . get ( index ) ) > 0 ) { swap ( elements , index - 1 , index ) ; lastSwapPosition = index ; } } position = lastSwapPosition ; } while ( position != 0 ) ; return elements ; }
Uses the Bubble Sort algorithm to sort a List of elements as defined by the Comparator or as determined by the elements in the collection if the elements are Comparable .