We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
privatestaticList<Person>personsList=newArrayList<>();publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. *///System.out.println("Input persons count(divided by space):");Scannersc=newScanner(System.in);intpersonsCount=sc.nextInt();inti=0;while(i<personsCount){Stringdata=sc.nextLine();if(!data.equals("")){String[]splittedData=data.split(" ");Stringname=splittedData[0];Integerscore=Integer.parseInt(splittedData[1]);personsList.add(newPerson(name,score));i++;}}sc.close();Collections.sort(personsList,newPersonComparator());for(Personp:personsList){System.out.println(p);}}}classPersonComparatorimplementsComparator<Person>{publicintcompare(Persono1,Persono2){if((o2.getScore()-o1.getScore())==0){returno1.getName().compareTo(o2.getName());}returno2.getScore()-o1.getScore();}}classPerson{privateStringname;privateIntegerscore;Person(Stringname,Integerscore){this.name=name;this.score=score;}publicvoidsetName(Stringname){this.name=name;}publicvoidsetScore(Integerscore){this.score=score;}publicStringgetName(){returnthis.name;}publicIntegergetScore(){returnthis.score;}publicStringtoString(){returnthis.name+" "+this.score;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sorting: Comparator
You are viewing a single comment's thread. Return to all comments →