亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按列對 CSV 進行排序

按列對 CSV 進行排序

揚帆大魚 2022-09-14 17:49:16
因此,我正在嘗試對到達時間的列進行排序,以便最早到達時間排在第一位。我是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
查看完整描述

2 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

我希望下面的這段代碼能幫助你:)我

做什么,我首先為每一行創建一個對象。之后,我將每個對象與我想與之比較的那行的鍵進行比較。這樣,您也可以選擇要比較的“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;


/**

 *

 * @author runef

 */

public class Test {


    public static void main(String[] args) {

        new Test();

    }


    private List<CoolObject> coolObjects = new ArrayList<>();

    private String[] linesOfCSV = {"A,B,C,D", "1,2,A,A", "2,1,B,B"};


    public Test() {

        String[] keys = null;

        for (String s : linesOfCSV) {

            String[] lineValues = s.split(",");

            if (keys == null) {

                keys = lineValues;

            } else {

                Map<String, String> aGoodName = new HashMap<>();

                for (int i = 0; i < lineValues.length; i++) {

                    //TODO: Check if keys[i] exists, check if lineValues[i] exists (if not probs something wrong with CSV)

                    aGoodName.put(keys[i], lineValues[i]);

                }

                coolObjects.add(new CoolObject(aGoodName));

            }

        }

        Collections.sort(coolObjects, new SortByKey("A"));

        System.out.println("SORTED BY KEY 'A'");

        for (CoolObject o : coolObjects) {

            for (Map.Entry<String, String> entry : o.getACoolMap().entrySet()) {

                System.out.print(entry.getKey() + ": " + entry.getValue() + "   ");

            }

            System.out.print("\n");

        }

        Collections.sort(coolObjects, new SortByKey("B"));

        System.out.println("SORTED BY KEY 'B'");

        for (CoolObject o : coolObjects) {

            for (Map.Entry<String, String> entry : o.getACoolMap().entrySet()) {

                System.out.print(entry.getKey() + ": " + entry.getValue() + "   ");

            }

            System.out.print("\n");

        }

    }


    class CoolObject {


        private Map<String, String> aCoolMap;


        public CoolObject(Map<String, String> aCoolMap) {

            this.aCoolMap = aCoolMap;

        }


        public Map<String, String> getACoolMap() {

            return aCoolMap;

        }

    }


    class SortByKey implements Comparator<CoolObject> {


        private String keySorter;


        public SortByKey(String keySorter) {

            this.keySorter = keySorter;

        }


        public int compare(CoolObject a, CoolObject b) {

            //TODO: CHECK IF KEY EXISTS IN BOTH VALUES! ELSE DO SOMETHING ELSE :) PROBS RETURN -1 SO IT COMES LAST!

            return a.getACoolMap().get(this.keySorter).hashCode() - b.getACoolMap().get(this.keySorter).hashCode();

        }

    }

}


查看完整回答
反對 回復 2022-09-14
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

這將對一列進行排序。您可以修改它以用于多個列。


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 {


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

        ArrayList<String> list = new ArrayList<>();

        int lineNumber = 0;

        File csvFile = new File("D://new.csv");

        BufferedReader br = new BufferedReader(new FileReader(csvFile));

        String line = "";

        while ((line = br.readLine()) != null) {

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

            list.add(arr[0]);

            lineNumber++;

        }

        Collections.sort(list);

        list.forEach(i -> System.out.println(i));

    }

}


查看完整回答
反對 回復 2022-09-14
  • 2 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號