如何在java

时间:2016-08-17 03:31:32

标签: java sorting csv arraylist comparison

我想从最小到最大排序整数,但我的输出显示 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;
        }
    };
 }

}

1 个答案:

答案 0 :(得分:1)

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

但是,您的代码通过创建可以使用Comparator<List<String>> sorter = Comparator .comparing(row -> row.get(0)) .thenComparingInt(row -> Integer.parse(row.get(2)); 中的方法本机创建的比较器来使事情过于复杂。

例如

{{1}}

在没有所有比较器创建功能的情况下,几乎可以完全执行您的代码。