gpt4 book ai didi

java - 如何在java中对csv文件中的整数列进行排序

转载 作者:行者123 更新时间:2023-11-30 07:12:31 26 4
gpt4 key购买 nike

我想将整数从小到大排序,但我的输出显示1、10、100,2、20、3、30、...

我需要将它们排序为 1,2,3,4,5,6.....10。请参阅下面我的代码。我假设我需要使用整数而不是字符串。我找不到如何更改代码。

public class sortcolumn {
private static final String COLUMN_SEPARATOR = ",";

public static void main(String[] args) throws Exception
{
String csv = "C:\\Users\\c0leese\\Desktop\\Top app\\August2016\\aPaidApp_2016-08-16.csv";
InputStream inputStream = new FileInputStream(csv);
List<List<String>> lines = readCsv(inputStream);

// Create a comparator that sorts primarily by column 0,
// and if these values are equal, by column 2
Comparator<List<String>> comparator = createComparator(0, 2);
Collections.sort(lines, comparator);

OutputStream outputStream = new FileOutputStream("C:\\Users\\c0leese\\Desktop\\Top app\\August2016\\test.csv");
String header = "Symbol, Exchange, Minimum, Average, Maximum, Total";
writeCsv(header, lines, outputStream);
}

private static List<List<String>> readCsv(
InputStream inputStream) throws IOException
{
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
List<List<String>> lines = new ArrayList<List<String>>();

// Skip header
String line = reader.readLine();

while (true)
{
line = reader.readLine();
if (line == null)
{
break;
}
List<String> list = Arrays.asList(line.split(COLUMN_SEPARATOR));
lines.add(list);
}
return lines;
}

private static void writeCsv(
String header, List<List<String>> lines, OutputStream outputStream)
throws IOException
{
Writer writer = new OutputStreamWriter(outputStream);
writer.write(header+"\n");
for (List<String> list : lines)
{
for (int i = 0; i < list.size(); i++)
{
writer.write(list.get(i));
if (i < list.size() - 1)
{
writer.write(COLUMN_SEPARATOR);
}
}
writer.write("\n");
}
writer.close();

}

private static <T extends Comparable<? super T>> Comparator<List<T>>
createComparator(int... indices)
{
return createComparator(sortcolumn.<T>naturalOrder(), indices);
}

private static <T extends Comparable<? super T>> Comparator<T>
naturalOrder()
{
return new Comparator<T>()
{
@Override
public int compare(T t0, T t1)
{
return t0.compareTo(t1);
}
};
}

private static <T> Comparator<List<T>> createComparator(
final Comparator<? super T> delegate, final int... indices)
{
return new Comparator<List<T>>()
{
@Override
public int compare(List<T> list0, List<T> list1)
{
for (int i = 0; i < indices.length; i++)
{
T element0 = list0.get(indices[i]);
T element1 = list1.get(indices[i]);
int n = delegate.compare(element0, element1);
if (n != 0)
{
return n;
}
}
return 0;
}
};
}

}

最佳答案

目前,您正在使用 createComparator 创建比较器进行排序,该比较器采用索引数组(转换为列号)。它没有给出每列的类型以及如何对它们进行排序的指示。您需要传入指示数据类型的内容。

但是,您的代码通过创建可以使用 Comparator 中的方法 native 创建的比较器,使事情变得过于复杂。

例如

Comparator<List<String>> sorter = Comparator
.comparing(row -> row.get(0))
.thenComparingInt(row -> Integer.parse(row.get(2));

在没有所有比较器创建函数的情况下,它几乎可以完成您的代码所做的事情。

关于java - 如何在java中对csv文件中的整数列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987667/

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