java数据结构排序输出问题

请问这些数据 在txt文档中读取
然后分别按照升序和降序排列输出到新的txt文档中,ps:(新的文档中
要有两次排序的结果)
求大神给出完整java代码,小弟弄了好久了都,

s1109853011,JackieChan,CS001,85
s0907451244,FranklinPapadias,MA002,65
s0308893477,MirandaLambert,HM467,35
s4045128481,SophiaPam,CM604,75
s0812340023,PhilipsChiu,CS001,71

2个回答

不知道这个是不是你要的

 public class SortData {
    public static void main(String[] args) {
        String oldPath = "C:/Users/东海/Desktop/foo.txt";
        String newPath = "C:/Users/东海/Desktop/bar.txt";
        try {
            sortAndWriteData(oldPath, newPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void sortAndWriteData(String path, String newPath)
            throws IOException {
        ArrayList<HashMap<String, String>> list = getData(path);
        writeToText(list, newPath);
        Collections.sort(list, new Comparator<HashMap<String, String>>() {

            @Override
            public int compare(HashMap<String, String> o1,
                    HashMap<String, String> o2) {
                if (o1.get("id").compareTo(o2.get("id")) > 0) {
                    return 1;
                }
                if (o1.get("id").compareTo(o2.get("id")) < 0) {
                    return -1;
                }
                return 0;
            }

        });
        writeToText(list, newPath);

    }

    private static void writeToText(ArrayList<HashMap<String, String>> list,
            String newPath) throws IOException {
        File file = new File(newPath);
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
        StringBuilder stringBuilder = new StringBuilder();
        for (HashMap<String, String> hashMap : list) {
            String data=stringBuilder.append(hashMap.get("id")+",")
                    .append(hashMap.get("name")+",").append(hashMap.get("unknown")+",")
                    .append(hashMap.get("num")).toString();
            bufferedWriter.write(data+"\r\n");
            bufferedWriter.flush();
            stringBuilder.delete(0, stringBuilder.length());
        }
        bufferedWriter.close();
    }

    private static ArrayList<HashMap<String, String>> getData(String path)
            throws IOException {
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        File file = new File(path);
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String content = "";
        while (content != null) {
            content = bufferedReader.readLine();
            if (content == null) {
                break;
            }
            String[] dataArray = content.split(",");
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("id", dataArray[0]);
            map.put("name", dataArray[1]);
            map.put("unknown", dataArray[2]);
            map.put("num", dataArray[3]);
            list.add(map);
            content = "";
        }
        bufferedReader.close();
        return list;
    }
}

 package com.cwn.wizbank.common;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * @Description:
 * @Author :小书包
 * @CreateDate :2018-11-19 18:00
 */
public class SortByFile {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
        String line;
        // 存储从文件中读取的对象
        ArrayList<Obj> list = new ArrayList<>();
        // 循环从文件中读取内容,并把值赋给对象
        while ((line = reader.readLine()) != null) {
            String[] splits = line.split(",");
            Obj obj = new Obj(splits[0], splits[1], splits[2], Integer.valueOf(splits[3]));
            list.add(obj);
        }
        // 收集需要写入文件的内容
        StringBuilder builder = new StringBuilder();
        // 按照升序排列
        Collections.sort(list, new Comparator<Obj>() {
            @Override
            public int compare(Obj o1, Obj o2) {
                return o1.equals(o2) ? 1 : -1;
            }
        });

        for (Obj obj : list) {
            builder.append(obj.toString());
        }
        // 按照降序排列
        Collections.sort(list, new Comparator<Obj>() {
            @Override
            public int compare(Obj o1, Obj o2) {
                return o1.equals(o2) ? -1 : 1;
            }
        });

        for (Obj obj : list) {
            builder.append(obj.toString());
        }

        // 将结果写入文件
        BufferedWriter writer = new BufferedWriter(new FileWriter("data.txt"));

        writer.write(builder.toString());
        // 关闭文件流
        writer.close();
        reader.close();
    }
}

class Obj {
    // id
    private String id;
    // 名称
    private String name;
    // 课程名
    private String course;
    // 分数
    private int score;

    public Obj(String id, String name, String course, int score) {
        this.id = id;
        this.name = name;
        this.course = course;
        this.score = score;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    // 重写equals,方便后期进行条件发生变更可直接修改
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Obj obj = (Obj) o;

        if (score != obj.score) return false;
        if (!id.equals(obj.id)) return false;
        if (!name.equals(obj.name)) return false;
        return course.equals(obj.course);

    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + name.hashCode();
        result = 31 * result + course.hashCode();
        result = 31 * result + score;
        return result;
    }
    // 创建toString方法,方便写入对象
    @Override
    public String toString() {
        return "" + id + "," + name + "," + course + "," + score + "\r\n";
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问