Skip to content

Instantly share code, notes, and snippets.

@annibuliful
Created April 23, 2018 07:27
Show Gist options
  • Save annibuliful/d4f35116c71449135cef8eb510a4f4cc to your computer and use it in GitHub Desktop.
Save annibuliful/d4f35116c71449135cef8eb510a4f4cc to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class main {
public String[] unsortString;
public String[] sortedString;
public static void main(String[] args){
String[] sorted = descendingSort(loadfile("data/name.txt"));
showString(loadfile("data/name.txt") , sorted);
}
static String[] loadfile(String fileName){
try {
BufferedReader file = new BufferedReader(new FileReader(fileName));
String sCurrentLine;
while ((sCurrentLine = file.readLine()) != null){
String[] splitString = sCurrentLine.split(",");
return splitString;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
static void showString(String[] unsort , String[] sorted){
System.out.println("[Original]");
System.out.print("[");
for(int i = 0 ; i < unsort.length; i++){
System.out.print(unsort[i] + " ");
}
System.out.print("]\n");
System.out.println("[Sorted]");
System.out.print("[");
for(int i = 0 ; i < sorted.length; i++){
System.out.print(sorted[i] + " ");
}
System.out.print("]");
}
static String[] descendingSort(String[] arr){
for ( int j = 0 ; j < arr.length ; j++ )
{
for( int i = j + 1 ; i < arr.length ; i++ )
{
if ( arr[i].compareTo( arr[j] ) > 0 )
{
String t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
}
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment