Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > it.comp.java > #9382

Re: sort array column 5

Path csiph.com!aioe.org!a+OD18ggP/kJ5LFzD5K8gA.user.gioia.aioe.org.POSTED!not-for-mail
From "Dr.UgoGagliardelli" <do.not.spam@me.please>
Newsgroups it.comp.java
Subject Re: sort array column 5
Date Wed, 10 Mar 2021 07:48:40 +0100
Organization Aioe.org NNTP Server
Lines 113
Message-ID <s29q0q$vck$1@gioia.aioe.org> (permalink)
References <541bbf2b-8a0c-4fe7-ba6b-f703ccca25f8n@googlegroups.com>
NNTP-Posting-Host a+OD18ggP/kJ5LFzD5K8gA.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0
Content-Language it
X-Notice Filtered by postfilter v. 0.9.2
X-Antivirus-Status Clean
X-Antivirus Avast (VPS 210308-0, 08/03/2021), Outbound message
Xref csiph.com it.comp.java:9382

Show key headers only | View raw


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 | NextPrevious in thread | Find similar


Thread

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