题解 | #成绩排序#
成绩排序
http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<person> result = new ArrayList<>(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int n = in.nextInt(); int sort = in.nextInt(); List<person> list = new ArrayList<>(); for(int i=0;i<n;i++){ String name = in.next(); int score = in.nextInt(); person p = new person(); p.name = name; p.score = score; list.add(p); } // 倒序 if ( sort == 0){ list.sort( (p2,p1) -> p1.score - p2.score ); } // 正序 else if (sort == 1) { list.sort( (p1,p2) -> p1.score - p2.score); } result.addAll(list); } for(person p : result){ System.out.println(p.name + " " + p.score); } } } class person { String name; int score; }