Buggy code:
@ Controller @ RequestMapping ( "view" ) public class UsersController { private int USERS_PER_PAGE = 20 ; @ RequestMapping public String showUsers ( PortletRequest request , PortletResponse response , SortFilterPage sortFilterPage , @ RequestParam ( value = "page" , required = false ) Long pageParam , @ RequestParam ( value = "memberCategory" , required = false ) String memberCategoryParam , Model model ) throws PortalException , SystemException { int page = 1 ; if ( pageParam != null ) page = pageParam . intValue ( ) ; int firstUser = 1 ; int usersCount = UserLocalServiceUtil . getUsersCount ( ) ; int pagesCount = usersCount // ( USERS_PER_PAGE ) ; int startPage = ( ( page - 5 ) > 0 ) ? page - 5 : 1 ; int endPage = ( ( startPage + 10 ) < pagesCount ) ? startPage + 10 : pagesCount ; if ( page > 1 ) firstUser = ( ( page - 1 ) * ( USERS_PER_PAGE ) ) + 1 ; int endUser = ( firstUser + ( USERS_PER_PAGE ) ) - 1 ; List < User_ > dBUsers = null ; String filter = "%" ; String filterParam = request . getParameter ( "filter" ) ; if ( filterParam != null ) { filter = ( "%" + filterParam ) + "%" ; sortFilterPage . setFilter ( filterParam ) ; } if ( ( memberCategoryParam == null ) || ( ( memberCategoryParam . compareTo ( "" ) ) == 0 ) ) if ( ( sortFilterPage . getSortColumn ( ) ) != null ) switch ( sortFilterPage . getSortColumn ( ) ) { case "USER_NAME" : if ( sortFilterPage . isSortAscending ( ) ) dBUsers = User_LocalServiceUtil . getUsersSortedByScreenNameAsc ( firstUser , endUser , filter ) ; else dBUsers = User_LocalServiceUtil . getUsersSortedByScreenNameDesc ( firstUser , endUser , filter ) ; break ; case "ACTIVITY" : Map < Long , Integer > activityCounts = ActivityUtil . getUsersActivityCount ( ) ; activityCounts = UsersController . sortByComparator ( activityCounts ) ; Object [ ] userIds = activityCounts . keySet ( ) . toArray ( ) ; dBUsers = new ArrayList < User_ > ( ) ; if ( sortFilterPage . isSortAscending ( ) ) { for ( int i = firstUser - 1 ; i <= ( endUser - 1 ) ; i ++ ) { Long userId = new Long ( new Long ( userIds [ i ] . toString ( ) ) ) ; User_ user_ = User_LocalServiceUtil . getUser_ ( userId ) ; dBUsers . add ( user_ ) ; } } else { for ( int i = endUser - 1 ; i >= ( firstUser - 1 ) ; i -- ) { Long userId = new Long ( new Long ( userIds [ i ] . toString ( ) ) ) ; User_ user_ = User_LocalServiceUtil . getUser_ ( userId ) ; dBUsers . add ( user_ ) ; } } break ; case "CATEGORY" : if ( sortFilterPage . isSortAscending ( ) ) dBUsers = User_LocalServiceUtil . getUsersSortedByRoleNameAsc ( firstUser , endUser , filter ) ; else dBUsers = User_LocalServiceUtil . getUsersSortedByRoleNameDesc ( firstUser , endUser , filter ) ; break ; case "MEMBER_SINCE" : if ( sortFilterPage . isSortAscending ( ) ) dBUsers = User_LocalServiceUtil . getUsersSortedByMemberSinceAsc ( firstUser , endUser , filter ) ; else dBUsers = User_LocalServiceUtil . getUsersSortedByMemberSinceDesc ( firstUser , endUser , filter ) ; break ; } else { dBUsers = User_LocalServiceUtil . getUsersSortedByScreenNameAsc ( firstUser , endUser , filter ) ; sortFilterPage . setSortColumn ( "USER_NAME" ) ; sortFilterPage . setSortAscending ( true ) ; } else { <START_BUG> usersCount = User_LocalServiceUtil . getUsersSortedByScreenNameAscFilteredByCategory ( 0 , Integer . MAX_VALUE , memberCategoryParam , filter ) . size ( ) ; <END_BUG> pagesCount = usersCount // ( USERS_PER_PAGE ) ; endPage = ( ( startPage + 10 ) < pagesCount ) ? startPage + 10 : pagesCount ; if ( ( sortFilterPage . getSortColumn ( ) ) != null ) switch ( sortFilterPage . getSortColumn ( ) ) { case "USER_NAME" : if ( sortFilterPage . isSortAscending ( ) ) dBUsers = User_LocalServiceUtil . getUsersSortedByScreenNameAscFilteredByCategory ( firstUser , endUser , filter , memberCategoryParam ) ; else dBUsers = User_LocalServiceUtil . getUsersSortedByScreenNameDescFilteredByCategory ( firstUser , endUser , filter , memberCategoryParam ) ; break ; case "ACTIVITY" : Map < Long , Integer > activityCounts = ActivityUtil . getUsersActivityCount ( ) ; activityCounts = UsersController . sortByComparator ( activityCounts ) ; Object [ ] userIds = activityCounts . keySet ( ) . toArray ( ) ; dBUsers = new ArrayList < User_ > ( ) ; if ( sortFilterPage . isSortAscending ( ) ) { for ( int i = firstUser - 1 ; i <= ( endUser - 1 ) ; i ++ ) { Long userId = new Long ( new Long ( userIds [ i ] . toString ( ) ) ) ; User_ user_ = User_LocalServiceUtil . getUser_ ( userId ) ; dBUsers . add ( user_ ) ; } } else { for ( int i = endUser - 1 ; i >= ( firstUser - 1 ) ; i -- ) { Long userId = new Long ( new Long ( userIds [ i ] . toString ( ) ) ) ; User_ user_ = User_LocalServiceUtil . getUser_ ( userId ) ; dBUsers . add ( user_ ) ; } } break ; case "CATEGORY" : if ( sortFilterPage . isSortAscending ( ) ) dBUsers = User_LocalServiceUtil . getUsersSortedByScreenNameAscFilteredByCategory ( firstUser , endUser , filter , memberCategoryParam ) ; else dBUsers = User_LocalServiceUtil . getUsersSortedByScreenNameDescFilteredByCategory ( firstUser , endUser , filter , memberCategoryParam ) ; break ; case "MEMBER_SINCE" : if ( sortFilterPage . isSortAscending ( ) ) dBUsers = User_LocalServiceUtil . getUsersSortedByMemberSinceAscFilteredByCategory ( firstUser , endUser , filter , memberCategoryParam ) ; else dBUsers = User_LocalServiceUtil . getUsersSortedByMemberSinceDescFilteredByCategory ( firstUser , endUser , filter , memberCategoryParam ) ; break ; }

Fix:
usersCount = User_LocalServiceUtil . getUsersSortedByScreenNameAscFilteredByCategory ( 0 , Integer . MAX_VALUE , filter , memberCategoryParam ) . size ( ) ;