My bubble sort method does not sort. It writes the same name again and again. It should sort Person(String name, int year) by year (now working)

I am new to programming and in a course to learn.

In this assignment have to make a class, Person(String name, int year). Then make a bubble sort, that sorts the Person objects by year. And I need to test it by writing the names in the console sorted by year.

I am soooo stuck and the same name is written in the console 5 times.

public class Person {
    private String name;
    private int year;

    public Person(String name, int year) {
        super();
        this.name = name;
        this.year = year;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public static void bubbleSort(Person[] list) {
        boolean undone;

        do {
            undone = false;
            
                for (int j = 0; j < list.length - 1; j++) {

                    if (list[j].getYear() > list[j + 1].getYear()) {
                        Person tmp = list[j];

                        list[j] = list[j + 1];
                        list[j + 1] = tmp;

                        undone = true;
                    }
                }
            
        } while (undone);
    }

    public static void printArray(Person[] myPerson) {
        int n = myPerson.length;
    
        for (int i = 0; i < n; ++i)
            System.out.println(myPerson[i].getName() + " ");
    }
}


public class MainPerson {

    public static void main(String[] args) {
        Person[] myPerson = new Person[] { new Person("Susanne Hansen", 1999), new Person("Hans Jespersen", 1896),
            new Person("Eva Gylden", 2789), new Person("Anders Aage", 1786), new Person("Ane Pulsar", 1345) };

        Person.bubbleSort(myPerson);
    
        Person.printArray(myPerson);
    }
}

Answer

Here is the updated Code which generates output as you require:

class Person {
    private  String name;
    private  int year;

    public Person(String name, int year) {
        this.name = name;
        this.year = year;
    }

    public  String getName() {
        return this.name;
    }

    public  void setName(String name) {
        this.name = name;
    }

    public  int getYear() {
        return year;
    }

    public  void setYear(int year) {
        this.year = year;
    }

    
}


public class Main {
    
    public static void bubbleSort(Person[] list) {
        boolean undone;

        do {
            undone = false;
            for (int i = 0; i < list.length - 1; i++) {
                for (int j = 0; j < list.length - 1; j++) {

                    if (list[j].getYear() > list[j + 1].getYear()) {
                        Person temp = list[j];
                        list[j] = list[j + 1];
                        list[j + 1] = temp;

                        undone = true;
                    }
                }
            }
        } while (undone);
    }

    public static void printArray(Person[] myPerson) {
        int n = myPerson.length;
    
        for (int i = 0; i < n; ++i)
            System.out.println(myPerson[i].getName() + " ");
    }

    public static void main(String[] args) {
        Person[] myPerson = new Person[] { new Person("Susanne Hansen", 1999), new Person("Hans Jespersen", 1896),
            new Person("Eva Gylden", 2789), new Person("Anders Aage", 1786), new Person("Ane Pulsar", 1345) };


        bubbleSort(myPerson);
    
        printArray(myPerson);
    }
}

There are few mistakes in your code:

  1. You need to store the value that you are replacing in a temporary variable in the bubble sort function.

Your Code is:

 if (list[j].getYear() > list[j + 1].getYear()) {
                        list[j] = list[j + 1];
                        list[j + 1] = list[j];

                        undone = true;
                    }

Need to update it to:

 if (list[j].getYear() > list[j + 1].getYear()) {
                        Person temp = list[j];
                        list[j] = list[j + 1];
                        list[j + 1] = temp;

                        undone = true;
                    }

What was happening in your code was as you didn’t store the value in a temporary variable the object at list[j] was getting lost.

  1. Never use static in POJO/Data classes. FYI POJO is a class that contains only data and functions to manipulate the data.

Using static will make java create the Object only once in memory and they will live throughout the lifecycle of the program.

  1. Using the Class name in the POJO class.

As you have used Person.name or Person.year it will create problem, always use this this.name/this.year keyword to reference objects that are inside the same class.

  1. Do not write functions that required to be called only once or twice inside Data/Pojo class. This will reduce the memory usage and running time.

You need to look into details of the Static Keyword,this Keyword and POJO Classes.