gpt4 book ai didi

java - 按一个字段对 csv 文件进行排序

转载 作者:行者123 更新时间:2023-11-30 02:35:49 27 4
gpt4 key购买 nike

尝试编写一个程序,使用compareTo对具有5个“字段”的csv文件进行排序,并且我已经得到了将csv文件导入到arrayList中的代码,以及其他一些功能。也就是说,我完全不知道需要什么代码来对填充的 arrayList 进行实际排序。

这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Student implements Comparable<Student>{

public static final String CSV_PATH = "unsortedList.csv";
public static int studentID, mark;
public static String fName, lName, grade;
public static boolean append = true;
public static ArrayList<String> aList = new ArrayList<String>();

public static void main(String[] args) throws IOException {

readAllLinesFromFile(CSV_PATH);
}

public static ArrayList<String> readAllLinesFromFile(String path) throws IOException{

FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while( (line = bufferedReader.readLine()) != null){
aList.add(line);
}
bufferedReader.close();
System.out.print(aList);

return aList;

}

public static void writeAllLinesToFile(String path, ArrayList<String> aList) throws IOException {
//ArrayList<String> aList = new ArrayList<String>();
FileWriter fileWriter = new FileWriter(path, append);
PrintWriter printWriter = new PrintWriter(fileWriter);
for (String line : aList){
printWriter.printf("%s" + "%n", line);
}
printWriter.close();
}

public Student(int studentID, String fName, String lName, int mark, String grade) {
super();
this.studentID = studentID;
this.fName = fName;
this.lName = lName;
this.mark = mark;
this.grade = grade;
}

public int getStudentID() {
return studentID;
}

public String getfName() {
return fName;
}

public String getlName() {
return lName;
}

public int getMark() {
return mark;
}

public String getGrade() {
return grade;
}

public int compareTo (Student s) {

if (this.mark == s.mark) {
return this.fName.compareTo(s.fName);
} else {
return (s.mark - this.mark) > 0 ? 1 : -1;
}
}
}

以下是 CSV 内容的示例:

StudentID,FirstName,LastName,FinalMark,FinalGrade
123464,John,Guest,77,P
123456,Dianne,Smith,76,P
122364,Stacey,Hobbs,58,P
123472,Laura,Taylor,67,P
123461,Lazarus,Wonton,42,F
123468,Nola,Emirate,50,P

非常感谢任何帮助!

最佳答案

看看这篇文章:How to sort an ArrayList in Java

在我看来,您应该重写比较函数,然后在数组列表上调用排序函数。

编辑:自己尝试过。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Main {

public static final String CSV_PATH = "unsortedList.csv";
public static boolean append = true;
public static ArrayList<String> aList = new ArrayList<String>();

public static void main(String[] args) throws IOException {

readAllLinesFromFile(CSV_PATH);
System.out.println("Unsorted:\n");
for(String aStudentString: aList){
System.out.println(aStudentString +"\n");
}

ArrayList<Student> students = convertToStudents(aList);
System.out.println("SORTED:\n");
for(Student student : students){
System.out.println(student.toString());
}
}

public static ArrayList<String> readAllLinesFromFile(String path) throws IOException{

FileReader fileReader = new FileReader(path);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while( (line = bufferedReader.readLine()) != null){
aList.add(line);
}
bufferedReader.close();

return aList;

}

public static ArrayList<Student> convertToStudents(ArrayList<String> studentsStrings) {
ArrayList<Student> students = new ArrayList<>();
studentsStrings.remove(0);
for(String studentString : studentsStrings) {
String[] parts = studentString.split(",");
int studentID = Integer.valueOf(parts[0]);
String fName = parts[1];
String lName = parts[2];
int mark = Integer.valueOf(parts[3]);
String grade = parts[4];
students.add(new Student(studentID, fName, lName, mark, grade));
}

Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.compareTo(o2);
}
});
return students;
}
public static void writeAllLinesToFile(String path, ArrayList<String> aList) throws IOException {
//ArrayList<String> aList = new ArrayList<String>();
FileWriter fileWriter = new FileWriter(path, append);
PrintWriter printWriter = new PrintWriter(fileWriter);
for (String line : aList){
printWriter.printf("%s" + "%n", line);
}
printWriter.close();
}
}

学生类(class):

public class Student implements Comparable<Student>{

public int studentID, mark;
public String fName, lName, grade;

public Student(int studentID, String fName, String lName, int mark, String grade) {
super();
this.studentID = studentID;
this.fName = fName;
this.lName = lName;
this.mark = mark;
this.grade = grade;
}

public int getStudentID() {
return studentID;
}

public String getfName() {
return fName;
}

public String getlName() {
return lName;
}

public int getMark() {
return mark;
}

public String getGrade() {
return grade;
}

@Override
public int compareTo (Student s) {

if (this.mark == s.mark) {
return this.fName.compareTo(s.fName);
} else {
return (s.mark - this.mark) > 0 ? 1 : -1;
}
}

@Override
public String toString() {
return "Student{" + "studentID=" + studentID + ", mark=" + mark + ", fName=" + fName + ", lName=" + lName + ", grade=" + grade + '}';
}

}

关于java - 按一个字段对 csv 文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43116496/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com