java - How to sort the array list? -
hey guys. thanx major regarding obstacles. problem time is, how can sort array list provided in code dont know whati need add in provied code below, simplyfive got 3 arraylist want make them 1 arraylist, can sorted in amont of guesses , tries( if 2 players have same guesses time should determine) . pretty hard explain tried best.............the best thing run figure whats issue is?
here code:
import java.util.*; import java.util.scanner.*; import java.util.arraylist.*; public class main { public static string tostring(int a, double b, string c) { string = integer.tostring(a); string bs = double.tostring(b); string scorelist = (as + "\t\t" + bs + "\t\t" + c); return scorelist; } private static void start() { int tries = 0 ; int guess = -1; string name ; string quit = "quit"; string y = "yes"; string n = "no"; string currentguess; string = ("y") ; scanner input = new scanner (system.in); arraylist<string> scores = new arraylist<string>(); boolean a=false; { a=false; int answer = (int) (math.random() * 1000 + 1) ; system.out.println( " welcome guessing game " ) ; system.out.print("please enter number between 1 , 1000 : "); currentguess = input.nextline(); long starttime = system.currenttimemillis(); { if (currentguess.equalsignorecase(quit)) { system.out.println("leaving soon?"); system.exit(0); } try { guess = integer.parseint(currentguess); } catch (numberformatexception nfe) { system.out.println(" dude can read, digits "); currentguess = input.nextline(); } if (guess < 1 || guess > 1000) { system.out.println("stupid guess wont count that."); currentguess = input.nextline(); } if (guess < answer ) { system.out.println("too low "+answer); currentguess = input.nextline(); tries++; } else if(guess > answer ) { system.out.println("too high "+answer); currentguess = input.nextline(); tries++; } else if (guess == answer) { //stop stop watch long endtime = system.currenttimemillis(); //calculate game time long gametime = endtime - starttime; system.out.println("you rock dude, job!"); system.out.println("you guessed " + tries + " times in " + (int)(gametime/1000) + " seconds."); system.out.println("please enter name."); name = input.nextline(); //create score object string tostring =tostring(tries, gametime, name); //add score arraylist scores.add(tostring); boolean s = false ; while (s==false) { system.out.print("want go again?(y/n)....."); currentguess = input.nextline(); if (currentguess.matches("y")) { system.out.println("highscore: \nguess \t time in milisec\tname"); //print out high score list (int = 0;i < scores.size(); i++) { system.out.println(scores.get(i)); } // main.start s=true; } //if user doesn't want play again if (currentguess.matches("n")) { system.out.println("highscore:\nguess \ttime in milisec\tname"); //print out high score list (int = 0;i < scores.size(); i++) { system.out.println(scores.get(i)); } system.out.println("thanx playing."); a=true; s=true; system.exit(0); } } } } while (guess != answer); }while(a==false); } public static void main(string[] args) { // arraylist<score> scores = new arraylist<score>(); main.start(); } }
use collections.sort()
, make sure class implements comparable
or use this:
collections.sort(list, new comparator<t>() { public int compare(t o1, t o2) { return o1.compareto(o2); }});
Comments
Post a Comment