0

I am using OpenCsv to access a fie which I wish to sort and then write back to another file name. Am having problem sorting the list. Thanks.

Am trying to sort the csv file by the 1st and 2nd columns.

 CSVReader reader = new CSVReader(new FileReader("d:\\temp\\data1.csv"), ',', '"', 1);

 //Read all rows at once
 List<String[]> allRows = reader.readAll();

 //Read CSV line by line and use the string array as you want
 for(String[] row : allRows){
    System.out.println(Arrays.toString(row));
 }

 *Arrays.sort(allRows.toArray());*

 CSVWriter writer = new CSVWriter(new FileWriter("d:\\temp\\data1sorted.csv"));
 writer.writeAll(allRows);

 //close the writer
 writer.close();

I am getting the following error when I run this code:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.Comparable
    at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.ComparableTimSort.sort(Unknown Source)
    at java.util.ComparableTimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at CompareCsv.main(CompareCsv.java:31)
Ibexy I
  • 1,123
  • 6
  • 16
  • 29
  • 1
    What is your actual problem? – Smutje Nov 27 '14 at 21:52
  • 2
    I suggest you look at `Collections.sort` like here: http://stackoverflow.com/questions/2839137/how-to-use-comparator-in-java-to-sort – maksimov Nov 27 '14 at 21:53
  • I have added the error am getting to the post. thanks for yr help – Ibexy I Nov 27 '14 at 22:17
  • Well, I guess you'll pick your data (Strings) out of the list. Let us what you want to do.. Do you want to sort word by word (by every word in your document) or line by line (like in a phonebook or something like that) – Martin Pfeffer Nov 28 '14 at 02:27
  • Thanks. The file is a csv with multi columns. I wanted to sort by the 1st and 2nd column. – Ibexy I Nov 28 '14 at 07:41

3 Answers3

1

Does this help you?

public static void main(String args[]) {
            ArrayList<String> animalList = new ArrayList<String>();

        animalList.add("Dog");
        animalList.add("Cat");
        animalList.add("Snake");
        animalList.add("Bison");

        System.out.println("Before Sorting:");
        for (String tmpStr : animalList) {
            System.out.println(tmpStr);
        }

        // sorting
        Collections.sort(animalList);

        System.out.println("After Sorting:");
        for (String tmpStr : animalList) {
            System.out.println(tmpStr);
        }
    }
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
0

You could implement a Comparator for your purpose and use: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)

0

It's not clear whether you want the rows sorted within themselves or compared to each other. Assuming both;

for(String[] row : allRows)
{
   Arrays.sort(row);
}

Collections.sort(allRows, new Comparator()
{
    @Override
    public int compare(Object o1, Object o2)
    {
        String[] a = (String[])o1;
        String[] b = (String[])o2;
        return a[0].compareTo(b[0]); //you'll probably want some additional checking here.
    }
});
Martin
  • 1,130
  • 10
  • 14