public static void sort ( List list , String sortBy , Map < String , FieldAccess > fields , boolean ascending , boolean nullsFirst ) { try { /* If this list is null or empty, we have nothing to do so return. */ if ( list == null || list . size ( ) == 0 ) { return ; } /* Grab the first item in the list and see what it is. */ Object o = list . get ( 0 ) ; /* if the sort by string is is this, and the object is comparable then use the objects themselves for the sort. */ if ( sortBy . equals ( "this" ) ) { Collections . sort ( list , thisUniversalComparator ( ascending , nullsFirst ) ) ; return ; } /* If you did not sort by "this", then sort by the field. */ final FieldAccess field = fields . get ( sortBy ) ; if ( field != null ) { Collections . sort ( list , Sorting . universalComparator ( field , ascending , nullsFirst ) ) ; } } catch ( Exception ex ) { Exceptions . handle ( ex , "list" , list , "\nsortBy" , sortBy , "fields" , fields , "ascending" , ascending , "nullFirst" , nullsFirst ) ; } }
This is the work horse . It does all of the sorting work for the simple cases .