I have to use selection sort to sort out a list of names and respective ages from a notepad file.
I have been able to implement the names and ages into array, and sort the names, but not the ages if there is a plural of the name and put a comma in between.
Please can you help me - my code is below.
import java.io.*; import java.util.*; public class MultipleKeySorting { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream("names_Age.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(in)); int maxIndx = -1; String a[] = new String[12]; for (int h = 0; h < a.length-1; h++) { maxIndx++; a[maxIndx] = br.readLine(); System.out.println(a[maxIndx]); } sort(a); System.out.println(""); print(a); } static String[] sort(String[] a) { String min; int minIndex; for (int i = 0; i < a.length; ++i) { min = a[i]; minIndex = i; for (int j = i + 1; j < a.length-1; j++) { //Find minimum if (min.charAt(0) > a[j].charAt(0)) { min = a[j]; minIndex = j; } } a[minIndex] = a[i]; //swap a[i] = min; } return a; } public static void print(String[] a) { // prints the elements of the specified array in sequence. if (a == null || a.length == 0) return; for (int i = 0; i < a.length-1; i++) System.out.println(a[i] + ", "); } }