Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | "Dr.UgoGagliardelli" <do.not.spam@me.please> |
|---|---|
| Newsgroups | it.comp.java |
| Subject | Re: sort array column 5 |
| Date | 2021-03-10 07:48 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <s29q0q$vck$1@gioia.aioe.org> (permalink) |
| References | <541bbf2b-8a0c-4fe7-ba6b-f703ccca25f8n@googlegroups.com> |
Il 09.03.2021 18.54, bender bender ha scritto:
> Hi,
> I'm scripting a java program that sort descending data from csv file
>
> import java.io.BufferedReader;
> import java.io.File;
> import java.io.FileReader;
> import java.io.IOException;
> import java.io.PrintStream;
> import java.util.Arrays;
>
>
> public class javaRes {
>
> // Sort column csv
>
> static class OrderColumn
>
> {
> public static void main (String[] args) throws IOException
>
> {
> File fileCSV = new File("Analisi_2018.csv");
> BufferedReader streamCSV = new BufferedReader(new FileReader(fileCSV));
>
> //Instantiating the File class
> File file = new File("provvi.csv");
> //Instantiating the PrintStream class
> PrintStream stream = new PrintStream(file);
> System.setOut(stream);
>
> String lineRead;
>
> int tmpRowCount=0;
>
> while( (lineRead=streamCSV.readLine()) != null )
> {
> tmpRowCount++;
> }
>
> streamCSV.close();
>
> System.out.println("Rowcount= "+tmpRowCount);
>
> String[] fileArray=new String[tmpRowCount];
>
> streamCSV = new BufferedReader(new FileReader(fileCSV));
>
> int tmpI=0;
>
> while( (lineRead=streamCSV.readLine()) != null )
>
> {
>
> fileArray[tmpI++]=lineRead;
>
> }
> //SORT HERE
>
>
> Arrays.sort(fileArray, Collections.reverseOrder());
>
> for (String number : fileArray) {
> System.out.println(number);
> }
>
>
>
> System.exit(1);
>
> }
>
> }
>
> }
>
> I obtain a new .csv
> but sorting about column 1
> I would order with target column 6
> How could fix it using this scheme of program
>
> regards
>
> A
>
Consider that Collections.reverseOrder() has type Comparator and you are
comparing String objects, so for that you sort for column 1 that's the
beginnig of the row. You shoud use a custom Comparator that compare
column 6. For example;
Comparator comparator = new Comparator<String>() {
public int compare(String o1, String o2) {
int comp = o2.compareTo(o1);
return comp;
}
}
for ascending order reverse the comparison to o1.compareTo(o2); so your
statement would be Arrays.sort(fileArray, comparator); tha's not
different from Collections.reverseOrder().
Now the problem is parsing strings to get 6th column from both o1 and o2
inside the method compare.
One trivial way could be compare the substring between 5th and 6th
comma-separator, e.g.:
public int compare(String o1, String o2) {
String[] a1 = o1.split(","), a2 = o2.split(",");
int comp = a2[5].compareTo(a1[5);
return comp;
}
but it's a weak way, as some text column can have the comma-separator
part of the text. Doing a stronger job is like reinventing the wheel:
there are plenty of java libraries, most of them open-source, that
extract columns from each row.
Back to it.comp.java | Previous | Next — Previous in thread | Find similar
sort array column 5 bender bender <voodoo.bender@gmail.com> - 2021-03-09 09:54 -0800 Re: sort array column 5 "Dr.UgoGagliardelli" <do.not.spam@me.please> - 2021-03-10 07:48 +0100
csiph-web