题解 | #成绩排序#
成绩排序
http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while(sc.hasNext()){
int n = sc.nextInt();
sc.nextLine();
int sortN = sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++){
String name = sc.next();
int grade = sc.nextInt();
list.add(name+" "+grade);
sc.nextLine();
}
if(sortN==0){
Collections.sort(list,new Comparator<String>(){
public int compare(String o1,String o2){
String[] o1s = o1.split(" ");
String[] o2s = o2.split(" ");
return Integer.parseInt(o2s[1]) - Integer.parseInt(o1s[1]);
}
});
}else if(sortN==1){
Collections.sort(list,new Comparator<String>(){
public int compare(String o1,String o2){
String[] o1s = o1.split(" ");
String[] o2s = o2.split(" ");
return Integer.parseInt(o1s[1]) - Integer.parseInt(o2s[1]);
}
});
}
for(String str: list){
System.out.println(str);
}
}
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
while(sc.hasNext()){
int n = sc.nextInt();
sc.nextLine();
int sortN = sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++){
String name = sc.next();
int grade = sc.nextInt();
list.add(name+" "+grade);
sc.nextLine();
}
if(sortN==0){
Collections.sort(list,new Comparator<String>(){
public int compare(String o1,String o2){
String[] o1s = o1.split(" ");
String[] o2s = o2.split(" ");
return Integer.parseInt(o2s[1]) - Integer.parseInt(o1s[1]);
}
});
}else if(sortN==1){
Collections.sort(list,new Comparator<String>(){
public int compare(String o1,String o2){
String[] o1s = o1.split(" ");
String[] o2s = o2.split(" ");
return Integer.parseInt(o1s[1]) - Integer.parseInt(o2s[1]);
}
});
}
for(String str: list){
System.out.println(str);
}
}
}
}