题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigDecimal; import java.math.BigInteger; import java.text.DateFormat; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.function.Function; import java.util.function.IntFunction; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; import static java.util.Arrays.*; import static java.util.stream.Stream.*; public class Main { public static void main(String[] args) throws IOException { testTh(); } private static void testTh() throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String str; str = bf.readLine(); int count=Integer.parseInt(str); str = bf.readLine(); int temp=Integer.parseInt(str); ArrayList<String> arrayList=new ArrayList<>(); int i=0; for (int j = 0; j < count; j++) { str = bf.readLine(); arrayList.add(str); } //升序 if (temp==1){ Collections.sort(arrayList, new Comparator<String>() { @Override public int compare(String o1, String o2) { String s1 = o1.split(" ")[1]; String s2 = o2.split(" ")[1]; int parseInt1 = Integer.parseInt(s1); int parseInt2 = Integer.parseInt(s2); return parseInt1-parseInt2; } }); }else { Collections.sort(arrayList, new Comparator<String>() { @Override public int compare(String o1, String o2) { String s1 = o1.split(" ")[1]; String s2 = o2.split(" ")[1]; int parseInt1 = Integer.parseInt(s1); int parseInt2 = Integer.parseInt(s2); return parseInt2-parseInt1; } }); } for (String s : arrayList) { System.out.println(s); } } }