题解 | #成绩排序#

成绩排序

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);
        }






    }





}

全部评论

相关推荐

06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务