Question: I need to sort the second column of a csv file by alphabetical order: I do not know how to sort the column. My program
I need to sort the second column of a csv file by alphabetical order:
I do not know how to sort the column. My program does not do that right now. It only prints the second column.
Sample from file:
1,Dance Monkey,Tones and I,50409214,https://open.spotify.com/track/1rgnBhdG2JDFTbYkYRZAku 2,The Box,Roddy Ricch,48937592,https://open.spotify.com/track/0nbXyq5TXYPCO7pr3N8S4I 3,Tusa,KAROL G,41434019,https://open.spotify.com/track/7k4t7uLgtOxPwTpFmtJNTY 4,Godzilla (feat. Juice WRLD),Eminem,40637125,https://open.spotify.com/track/7FIWs0pqAYbP91WWM0vlTQ 5,Yummy,Justin Bieber,34586419,https://open.spotify.com/track/41L3O37CECZt3N7ziG2z7l 6,Life Is Good (feat. Drake),Future,34200687,https://open.spotify.com/track/5yY9lUy8nbvjM1Uyo1Uqoc 7,Blinding Lights,The Weeknd,33551309,https://open.spotify.com/track/0sf12qNH5qcw8qpgymFOqD 8,ROXANNE,Arizona Zervas,32102084,https://open.spotify.com/track/696DnlkuDOXcMAnKlTgXXK 9,Falling,Trevor Daniel,28606316,https://open.spotify.com/track/4TnjEaWOeW0eKTKIEvJyCa 10,Don't Start Now,Dua Lipa,28228768,https://open.spotify.com/track/6WrI0LAC5M1Rw2MnX2ZvEg 11,Memories,Maroon 5,24516744,https://open.spotify.com/track/2b8fOow8UzyDFAE27YhOZM 12,Circles,Post Malone,23997809,https://open.spotify.com/track/21jGcNKet2qwijlDFuPiPb 13,everything i wanted,Billie Eilish,21786149,https://open.spotify.com/track/3ZCTVFBt2Brf31RLEnCkWJ 14,Good News,Mac Miller,21727425,https://open.spotify.com/track/1DWZUa5Mzf2BwzpHtgbHPY
So sample output should be:
Arizona
Billie Eilish
Dua Lipa
etc...
Here is my code: (Needs to be done in JAVA
import java.io.*;
import java.util.*;
public class sorting {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new BufferedReader(new FileReader("csvFiles.csv")));
int rows = 200;
int cols = 4;
String [][] arr = new String[cols][rows];
while(sc.hasNextLine()){
for(int i=0; i < arr.length; i++ ){
String[] line = sc.nextLine().split(",");
for(int j=0; j
arr[i][j] = line[j];
}
line[2] = arr[i][2];
System.out.println(line[2]);
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
