Created
April 23, 2018 07:27
-
-
Save annibuliful/d4f35116c71449135cef8eb510a4f4cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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