按值排序列 CSV Java

我有一个由两个属性组成的 csv 文件:第一个是 string 类型,第二个是 double 类型。


从这个 csv 文件开始,我想获得另一个,但是,根据第二个属性的值越来越多地排序。在 SQL 中有ORDER BY允许根据指定属性对数据库进行排序的函数,我希望得到与ORDER BY.


示例输入 CSV 文件:


tricolor;14.0

career;9.0

salty;1020.0

looks;208.0

bought;110.0

预期的输出 CSV 文件:


career;9.0

tricolor;14.0

bought;110.0

looks;208.0

salty;1020.0


慕丝7291255
浏览 121回答 3
3回答

HUH函数

将逗号分隔的值放入LinkedHashMapTreeMap<String, Double> map = new LinkedHashMap<String, Double>();try (BufferedReader br = Files.newBufferedReader(Paths.get("yourfile.csv"))) {&nbsp; &nbsp; String line;&nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; String[] parts = line.split(";");&nbsp; &nbsp; &nbsp; &nbsp; map.put(parts[0], Double.parseDouble(parts[1]));&nbsp; &nbsp; }}catch (IOException e) {&nbsp; &nbsp; System.err.format("IOException: %s%n", e);}map然后根据双精度值排序。尝试使用 java 8,LinkedHashMap<String, Double> sortedMap;sortedMap = map.entrySet().stream().sorted(Entry.comparingByValue()).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

烙印99

我们可以尝试将文件解析为排序映射(例如TreeMap)的一般方法,然后迭代映射并写回文件。TreeMap<String, Double> map = new TreeMap<String, Double>();try (BufferedReader br = Files.newBufferedReader(Paths.get("yourfile.csv"))) {&nbsp; &nbsp; String line;&nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; String[] parts = line.split(";");&nbsp; &nbsp; &nbsp; &nbsp; map.put(parts[0], Double.parseDouble(parts[1]));&nbsp; &nbsp; }}catch (IOException e) {&nbsp; &nbsp; System.err.format("IOException: %s%n", e);}// now write the map to file, sorted ascending in alphabetical ordertry (FileWriter writer = new FileWriter("yourfileout.csv");&nbsp; &nbsp; BufferedWriter bw = new BufferedWriter(writer)) {&nbsp; &nbsp; for (Map.Entry<String, Double> entry : map.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; bw.write(entry.getKey() + ";" + entry.getValue());&nbsp; &nbsp; }}catch (IOException e) {&nbsp; &nbsp; System.err.format("IOException: %s%n", e);}笔记:我假设第一列中的字符串值总是唯一的。如果可能存在重复,则必须修改上述脚本以使用列表映射或类似的东西。我还假设字符串值都是小写的。如果没有,那么您可能无法获得您期望的排序。如果这是一个问题,一种解决方案是在将该键插入映射之前将每个字符串小写(或大写)。

呼唤远方

将 CSV 文件读入一个( CSV 文件中List的每行一个)Object[]Object[]数组的第一个元素是行本身(一个字符串)数组的第二个元素是 double 的值(一个 Double)所以你有以下列表:{&nbsp;&nbsp; ["tricolor;14.0", 14.0],&nbsp;&nbsp; ["career;9.0", 9.0],&nbsp;&nbsp; ["salty;1020.0", 1020.0],&nbsp; ["looks;208.0", 208.0],&nbsp; ["bought;110.0", 110.0]}然后根据double的值排序然后您可以将其写回 CSV 文件(仅写入每个数组的第一个元素)List<Object[]> list = readFile("myFile.csv");list.sort(Comparator.comparing(p -> (Double)p[1]));// write to csv file, just printing it out herelist.forEach(p -> System.out.println(p[0]));读取文件的方法:private static List<Object[]> readFile(String fileName) {&nbsp; &nbsp; List<Object[]> list = new ArrayList<>();&nbsp; &nbsp; try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {&nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; String[] splitLine;&nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; splitLine = line.split(";");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add an array, first element is the line itself, second element is the double value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(new Object[] {line, Double.valueOf(splitLine[1])});&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return list;}编辑如果你想要相反的顺序:一旦你有你的排序列表,你可以使用类reverse上的方便方法来反转它CollectionsCollections.reverse(list);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java