You can also check out my Pac-Man scenario. It has two separate highscores for two values (score and level). There is no nearby ranking though. The sorting algorithm isn’t really optimized, but it works well.
import java.util.*; public class check_HIGHEST_NUM { public static void main() { ArrayList<Integer> yourList=new ArrayList<Integer>(1000); ArrayList<Integer> othersList=new ArrayList<Integer>(1000); ArrayList<Integer> globalList=new ArrayList<Integer>(1000); int n=0,copy=0,count=0; int n1=0,copy1=0,count1=0; int check=0; Scanner sc=new Scanner(System.in); System.out.println("Your num"); do { n=sc.nextInt(); yourList.add(n); globalList.add(n); count++; }while(count<=0); System.out.println("Other's num"); do { n1=sc.nextInt(); othersList.add(n1); globalList.add(n1); count1++; }while(count1<=2); Integer max1=Collections.max(yourList); Integer max2=Collections.max(othersList); if(max2>max1) check=max2; else if(max1>max2) check=max1; Collections.sort(globalList, Collections.reverseOrder()) ; System.out.println("@Your_Score"+yourList+"\n"+"@Other's Score"+othersList+"\n"+"@Highest_Score"+check+"\n"+"@Top scores in dec order"+globalList); } }
for (int i=1; i<list.size(); i++) { int n = i; while (n > 0 && list.get(n).getInt(game) > list.get(n-1).getInt(game)) { java.util.Collections.swap(list, n, n-1); } n--; }
private void drawScores(int game) { final int pixelsPerUser = 50+2*GAP; final int numUsers = ((getImage().getHeight()-(HEADER_TEXT_HEIGHT+10))/pixelsPerUser); final int topSpace = getImage().getHeight()-(numUsers*pixelsPerUser)-GAP; getImage().setColor(BACKGROUND_COLOR); getImage().fill(); drawString("Top Players", 120, topSpace-HEADER_TEXT_HEIGHT-5, MAIN_COLOR, HEADER_TEXT_HEIGHT); drawString("Your Score", 120 + getImage().getWidth()/2, topSpace-HEADER_TEXT_HEIGHT-5, MAIN_COLOR, HEADER_TEXT_HEIGHT); List<UserInfo> list = UserInfo.getTop(200); for (int i=1; i<list.size(); i++) { int n = i; while (n > 0 && list.get(n).getInt(game) > list.get(n-1).getInt(game)) java.util.Collections.swap(list, n, (n--)-1); } List<UserInfo> tops = list.subList(0, Math.min(numUsers, list.size())); drawUserPanel(game, GAP, topSpace, (getImage().getWidth()/2)-GAP, topSpace+numUsers*pixelsPerUser, tops, 0); UserInfo me = UserInfo.getMyInfo(); int rank = 0; for ( ; rank<list.size(); rank++) if (list.get(rank).getUserName().equals(me.getUserName())) break; tops = list.subList(rank, rank+1); drawUserPanel(game, getImage().getWidth()/2+GAP, topSpace, getImage().getWidth()-GAP, topSpace+1+pixelsPerUser, tops, rank); } private void drawUserPanel(int game, int left, int top, int right, int bottom, List users, int startRank) { getImage().setColor(MAIN_COLOR); getImage().drawRect(left, top, right-left, bottom-top); if (users == null) return; UserInfo me = UserInfo.getMyInfo(); int y = top + GAP; for (Object obj : users) { UserInfo user = (UserInfo)obj; if (user == null) continue; Color c = (me != null && user.getUserName().equals(me.getUserName())) ? BACKGROUND_HIGHLIGHT_COLOR : BACKGROUND_COLOR; getImage().setColor(c); getImage().fillRect(left+5, y-GAP+1, right-left-10, 50+2*GAP-1); int x = left + 10; drawString("#"+Integer.toString(users.indexOf(user)+1+startRank), x, y+18, MAIN_COLOR, 14); x += 50; drawString(Integer.toString(user.getInt(game)), x, y+18, SCORE_COLOR, 14); x += 80; getImage().drawImage(user.getUserImage(), x, y); x += 55; drawString(user.getUserName(), x, y+18, MAIN_COLOR, 14); y += 50+2*GAP; } }