Search code examples
javaarraylistselection-sort

Why is my ArrayList of String[]'s not sorting properly?


I am reading data in from a file, and asking the user which category they would like to sort by. The file has 13 columns (starting with country, year, and continent, then a list of double statistics that I am ignoring for the time being) and 792 rows. I have had no trouble initializing the ArrayList<String[]>, and I have a method to print out the rows and they are formatted correctly.

I am trying to sort the ArrayList based on one of the columns (I am using the first column right now as comparing the Country names is the easiest to see, and the original file is already organized by year, so that would not be a good way to tell if the sorting worked). I currently am trying to implement a selectionSort method which appears to work alright, but when I print out the rows of the modified ArrayList, there are several rows that are out of order (but it appears that many sections have been better organized).

Here are a few lines from the file for reference when converted to a String[]:

[ Singapore 2015 AS 6.572000027 1.69227767 1.353814363 0.949492395 0.549840569 0.345965981 0.464307785 85 ]

[ Netherlands 2015 EU 7.376999855 1.503944635 1.428939223 0.810696125 0.585384488 0.47048983 0.282661825 84 ]

[ Canada 2015 NA 7.315999985 1.479204416 1.481348991 0.834557652 0.611100912 0.435539722 0.287371516 83 ]

Here is the chunk of code for the selectionSort method that I am having an issue with. ArrayList<String[]> data is the data being read in, and int sortIndex for the Country example is zero, the column of each individual String[] that I am trying to compare.

public static void selectionSort(ArrayList<String[]> data, int sortIndex){
    int i;
    int j;
    String[] temp;
    int indexSmallest = 0;

    for (i = 0; i < data.size()-1; i++) {
        indexSmallest = i;
            
        for (j= i + 1; j < data.size(); j++) {
            if (data.get(j)[sortIndex].compareTo(data.get(i)[sortIndex]) < 0) {
                indexSmallest = j;
            }
        }
        temp = data.get(i);
        data.set(i, data.get(indexSmallest));
        data.set(indexSmallest, temp);
    }
}

I am new to stack overflow, so if there is any additional information I should include, please let me know! I have read through a couple similar questions but have not been able to figure out what is wrong with my process. Thanks in advance!

Update: Here is my initialization of the ArrayList, called dataAL in my main method:

while (currLine != null) {
    dataAL.add(currLine.split("\\,", -1));
    lineNum++;
    currLine = readBuffer.readLine();
}

I output the arrays with a separate printRow(String[] row) method which simply prints each row passed to the method separated by spaces and with hard brackets on each end (as seen in the example rows I listed above). The ArrayList<String[]> is called dataAL, but when passed to the sort method it is named data, which is then passed to the insertion sort method as data again. Here is the printing of the data:

for (int k = 0; k < data.size(); k++){
    printRow(data.get(k));
}

I am expecting that as the rows print out, the rows are organized alphabetically by index 0 of each String[], which is the country names that I am comparing, and while some sections are in alphabetical order, the entire list is not.


Solution

  • Don't complicate things unnecessarily. You can use the sort method of List, which accepts a comparator:

    public static void main(String[] args) {
        List<String[]> myList = new ArrayList<>();
        myList.add(new String[] {"Singapore", "2015", "AS", "6.572000027", "1.69227767", "1.353814363", "0.949492395",
                                 "0.549840569", "0.345965981", "0.464307785", "85"});
        myList.add(new String[] {"Netherlands", "2015", "EU", "7.376999855", "1.503944635", "1.428939223", "0.810696125",
                              "0.585384488", "0.47048983", "0.282661825", "84"});
        myList.add(new String[] {"Canada", "2015", "NA", "7.315999985", "1.479204416", "1.481348991", "0.834557652",
                                 "0.611100912", "0.435539722", "0.287371516", "83"});
    
        System.out.println("Before sorting");
        myList.forEach(arr -> System.out.println(Arrays.toString(arr)));
        
        sortByIndex(myList, 0);
    
        System.out.println("After sorting");
        myList.forEach(arr -> System.out.println(Arrays.toString(arr)));
    }
    
    public static void sortByIndex(List<String[]> data, int sortIndex) {
        data.sort(Comparator.comparing(d -> d[sortIndex]));
    }
    

    Of course, you may have to check beforehand whether the index is valid.