按列对 CSV 进行排序

因此,我正在尝试对到达时间的列进行排序,以便最早到达时间排在第一位。我是java中csv文件的新手,所以挣扎着大时间。


我已经设法读取csv文件并使用数组打印,但不确定如何对特定列进行排序


import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Scanner;


public class readCSV {

 public static void main(String[] args) {

    String fileName= "csv.csv";

    File file= new File(fileName);

    // this gives you a 2-dimensional array of strings

    List<List<String>> lines = new ArrayList<>();

    Scanner inputStream;


    try{

        inputStream = new Scanner(file);

        while(inputStream.hasNext()){

            String line= inputStream.next();

            String[] values = line.split(",");

            // this adds the currently parsed line to the 2-dimensional string array

            lines.add(Arrays.asList(values));  

            //System.out.println(line);   

            System.out.println(values[0] + ' ' + values[1] + ' ' + values[2] + ' ' + values[3] ); 

        }

        inputStream.close();

    }catch (FileNotFoundException e) {

        e.printStackTrace();

    }


    // the following code lets you iterate through the 2-dimensional array

    /*int lineNo = 1;

    for(List<String> line: lines) {

    int columnNo = 1;

    for (String value: line) {

    System.out.println("Line " + lineNo + " Column " + columnNo + ": " + value);

    columnNo++;

    }

    lineNo++;

    }*/

 }

}

如果有任何其他改进,例如存储csv,打印等,我很乐意更改它


下面是一个输入示例:


  processID arrivalTime burstTime priority

    1 0 5 1

    2 1 7 2

    3 0 2 1

    4 2 6 2

    5 6 10 3

    6 5 4 4

    7 6 4 7

    8 5 4 8

    9 6 6 3

    10 6 7 2


扬帆大鱼
浏览 143回答 2
2回答

守着一只汪

我希望下面的这段代码能帮助你:)我做什么,我首先为每一行创建一个对象。之后,我将每个对象与我想与之比较的那行的键进行比较。这样,您也可以选择要比较的“CSV密钥”。您可以使用此代码,但请记住,我在那里放置了2个“TODO”,如果您不考虑它们,这些可能是空指针异常:)import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;/**&nbsp;*&nbsp;* @author runef&nbsp;*/public class Test {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new Test();&nbsp; &nbsp; }&nbsp; &nbsp; private List<CoolObject> coolObjects = new ArrayList<>();&nbsp; &nbsp; private String[] linesOfCSV = {"A,B,C,D", "1,2,A,A", "2,1,B,B"};&nbsp; &nbsp; public Test() {&nbsp; &nbsp; &nbsp; &nbsp; String[] keys = null;&nbsp; &nbsp; &nbsp; &nbsp; for (String s : linesOfCSV) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] lineValues = s.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (keys == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keys = lineValues;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> aGoodName = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < lineValues.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //TODO: Check if keys[i] exists, check if lineValues[i] exists (if not probs something wrong with CSV)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aGoodName.put(keys[i], lineValues[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coolObjects.add(new CoolObject(aGoodName));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(coolObjects, new SortByKey("A"));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("SORTED BY KEY 'A'");&nbsp; &nbsp; &nbsp; &nbsp; for (CoolObject o : coolObjects) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Map.Entry<String, String> entry : o.getACoolMap().entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(entry.getKey() + ": " + entry.getValue() + "&nbsp; &nbsp;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(coolObjects, new SortByKey("B"));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("SORTED BY KEY 'B'");&nbsp; &nbsp; &nbsp; &nbsp; for (CoolObject o : coolObjects) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Map.Entry<String, String> entry : o.getACoolMap().entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(entry.getKey() + ": " + entry.getValue() + "&nbsp; &nbsp;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\n");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class CoolObject {&nbsp; &nbsp; &nbsp; &nbsp; private Map<String, String> aCoolMap;&nbsp; &nbsp; &nbsp; &nbsp; public CoolObject(Map<String, String> aCoolMap) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.aCoolMap = aCoolMap;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Map<String, String> getACoolMap() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return aCoolMap;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class SortByKey implements Comparator<CoolObject> {&nbsp; &nbsp; &nbsp; &nbsp; private String keySorter;&nbsp; &nbsp; &nbsp; &nbsp; public SortByKey(String keySorter) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.keySorter = keySorter;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int compare(CoolObject a, CoolObject b) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //TODO: CHECK IF KEY EXISTS IN BOTH VALUES! ELSE DO SOMETHING ELSE :) PROBS RETURN -1 SO IT COMES LAST!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return a.getACoolMap().get(this.keySorter).hashCode() - b.getACoolMap().get(this.keySorter).hashCode();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

拉莫斯之舞

这将对一列进行排序。您可以修改它以用于多个列。import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;public class Test {&nbsp; &nbsp; public static void main(String[] args) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; int lineNumber = 0;&nbsp; &nbsp; &nbsp; &nbsp; File csvFile = new File("D://new.csv");&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader br = new BufferedReader(new FileReader(csvFile));&nbsp; &nbsp; &nbsp; &nbsp; String line = "";&nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] arr = line.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(arr[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineNumber++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(list);&nbsp; &nbsp; &nbsp; &nbsp; list.forEach(i -> System.out.println(i));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java