Search code examples
javaarraylistbubble-sort

Using bubbles sort to sort ArrayList of (String, Int) to alphabetical order


I am trying to sort the babies in alphabetical order in the sortBabies using bubblesort but i unable to swap the position as the error came up as "The methods set(int, Baby) in the type ArrayList is not applicable for the arguments(int, string)". I set the name 1 and name 2 to string value so i am able to compare but now i am unable to swap the position.

import java.util.*;
public class BabyCilent {
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList<Baby> babies = new ArrayList<Baby>();
    
    /*for(int i = 0; i < 4; i++) {
        Baby newBaby = getBaby();
        babies.add(newBaby);
    }*/
    
    //hardcode data so that dont need to keep entering
    
    Baby baby1 = new Baby("d", 4);
    Baby baby2 = new Baby("c", 3);
    Baby baby3 = new Baby("a", 1);
    Baby baby4 = new Baby("b", 2);
    babies.add(baby1);
    babies.add(baby2);
    babies.add(baby3);
    babies.add(baby4);
    
    int choice = 0;
    while(choice != 7){
       System.out.println("1. Print all the baby in the array list");
       System.out.println("2. Calculate the average age for all the baby");
       System.out.println("3. Remove the baby in the array listn");
       System.out.println("4. Add one more baby into the array list");
       System.out.println("5. Sort all the baby in alphabetical order");
       System.out.println("6. Output sorted baby to CSV file");
       System.out.println("7. Exit");
        choice = scanner.nextInt();
        if(choice == 1) {
            printAllBabies(babies);
        } 
        if(choice == 2) {
            calculateAveAge(babies);
        }
        if(choice == 3){
            removeBaby(babies);
        } 
        if(choice == 4){
            addInOneMoreBaby(babies);
        } 
        if(choice == 5) {
            sortBabies(babies);
        }
        if(choice == 6) {
            toCSVFile(babies);
        }
    }   
}

private static void sortBabies(ArrayList<Baby> babies) {
    boolean swap = true;
    while(swap) {
        swap = false;
        for(int i = 1; i < babies.size(); i++) {
            String name1 = babies.get(i-1).getName();
            String name2 = babies.get(i).getName();
            if(name1.compareTo(name2)>0) { //swap the two strings
                babies.set(i-1, name2);
                babies.set(i, name1);
                swap = true;
            }
        }
    }
}

Solution

  • You want to change the inner of your for loop in oder to set the elements to childs and not to string while swapping.

    Your list is the type of Baby and you want to change elements in the list by the name of the baby. The problem with that is that the type of a name is a String and therefor missmatches the type Baby. In oder to fix this you can change the local variables to Baby instead of String and just use getName() in your if to compaire them as shown below:

    for(int i = 1; i < babies.size(); i++) {
        Baby baby1 = babies.get(i-1);
        Baby baby2 = babies.get(i);
        if(baby1.getName().compareTo(baby2.getName())>0) { //swap the two strings
            babies.set(i-1, baby2);
            babies.set(i, baby1);
            swap = true;
        }
    }