Sort list of Objects Java

I have a little problem, i know i can use sort functions from Libraries. But this is a task from my University and i should write a sort function for my own. So i have Objects of students in my List.

Student(String Prename, String Surname, Int ID, Int gender , String Course)

Student s1 = new Student("Steven", "Schrott", 533546, 1 , "AI");

so now i have 4 Students in my list and now i have to Sort this list by Prenames.

This is my Sort function but it doesnt work.

public static List<Studen> sortStudenPrename(List<Student> data){
  List<Student> studenList = new ArrayList<StudenT>();
  for(int i = 0 ; i < data.size(); i++){
    for(int j = 0; j < data.size(); j ++){
       if(data.get(i).getPrename().compareTo(data.get(j).getPrename()) > 0) {
          studentList.add(data.get(i));
       }
    }
  }
  return studentList;
}  

my output ist than like this and no every Name only is once in my list, but my Sort function kinda puts them more than once inside the new list.

  • Dennis
  • Nico
  • Dan
  • Dan
  • Dennis
  • Dan

Answer

You can implement your own Quick-Sort algorithm.

There is a great example here:

Using comparator in custom quicksort in java

Driver

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Driver {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<Student>();

        students.add(new Student(1, "Dennis", "", Gender.MALE, ""));
        students.add(new Student(2, "Nico",   "", Gender.MALE, ""));
        students.add(new Student(3, "Dan",    "", Gender.MALE, ""));
        students.add(new Student(4, "Dan",    "", Gender.MALE, ""));
        students.add(new Student(5, "Dennis", "", Gender.MALE, ""));
        students.add(new Student(6, "Dan",    "", Gender.MALE, ""));

        // This works, if you add the Comparable interface the class.
        // SortingUtils.quickSort(students); 

        // Most of the time, you will have a custom Comparator
        SortingUtils.quickSort(students, new Comparator<Student>() {
            @Override
            public int compare(Student student1, Student student2) {
                int diff = student1.getGivenName().compareTo(student2.getGivenName());
                if (diff != 0) return diff;
                return Long.compare(student1.getId(), student2.getId());
            }
        });

        for (Student student : students) {
            System.out.println(student);
        }
    }
}

Output

Student [id=3, givenName=Dan, surname=, gender=MALE, course=]
Student [id=4, givenName=Dan, surname=, gender=MALE, course=]
Student [id=6, givenName=Dan, surname=, gender=MALE, course=]
Student [id=1, givenName=Dennis, surname=, gender=MALE, course=]
Student [id=5, givenName=Dennis, surname=, gender=MALE, course=]
Student [id=2, givenName=Nico, surname=, gender=MALE, course=]

SortingUtils

import java.util.Comparator;
import java.util.List;

public class SortingUtils {
    public static <T> List<T> quickSort(List<T> list, Comparator<T> comparator) {
        return quickSort(list, comparator, 0, list.size() - 1);
    }

    public static <T extends Comparable<T>> List<T> quickSort(List<T> list) {
        return quickSort(list, 0, list.size() - 1);
    }

    private static <T> List<T> quickSort(List<T> list, Comparator<T> comparator, int left, int right) {
        int ll = left;
        int rr = right;

        if (rr > ll) {
            T pivot = list.get((ll + rr) / 2);
            while (ll <= rr) {
                while (ll < right && comparator.compare(list.get(ll),  pivot) < 0) {
                    ll += 1;
                }
                while (rr > left && comparator.compare(list.get(rr),  pivot) > 0) {
                    rr -= 1;
                }
                if (ll <= rr) {
                    swap(list, ll, rr);
                    ll += 1;
                    rr -= 1;
                }
            }
            if (left < rr)  quickSort(list, comparator, left, rr);
            if (ll < right) quickSort(list, comparator, ll, right);
        }

        return list;
    }

    private static <T extends Comparable<T>>List<T> quickSort(List<T> list, int left, int right) {
        int ll = left;
        int rr = right;

        if (rr > ll) {
            T pivot = list.get((ll + rr) / 2);
            while (ll <= rr) {
                while (ll < right && list.get(ll).compareTo(pivot) < 0) {
                    ll += 1;
                }
                while (rr > left && list.get(rr).compareTo(pivot) > 0) {
                    rr -= 1;
                }
                if (ll <= rr) {
                    swap(list, ll, rr);
                    ll += 1;
                    rr -= 1;
                }
            }
            if (left < rr)  quickSort(list, left, rr);
            if (ll < right) quickSort(list, ll, right);
        }

        return list;
    }

    private static <T> void swap(List<T> list, int left, int right) {
        T temp = list.get(left);
        list.set(left, list.get(right));
        list.set(right, temp);
    }
}

Student

public class Student implements Comparable<Student> {
    private long id;
    private String givenName;
    private String surname;
    private Gender gender;
    private String course;

    public Student(long id, String givenName, String surname, Gender gender, String course) {
        this.id = id;
        this.givenName = givenName;
        this.surname = surname;
        this.gender = gender;
        this.course = course;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String givenName) {
        this.givenName = givenName;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    @Override
    public String toString() {
        return String.format("Student [id=%s, givenName=%s, surname=%s, gender=%s, course=%s]", id, givenName, surname,
                gender, course);
    }

    /** Usually logic is left out of a POJO. */
    @Override
    public int compareTo(Student student) {
        return this.getGivenName().compareTo(student.getGivenName());
    }
}

Gender

public enum Gender {
    UNKNOWN,
    MALE,
    FEMALE
}

Leave a Reply

Your email address will not be published. Required fields are marked *