题解 | #成绩排序#

成绩排序

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






    }





}

全部评论

相关推荐

迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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