-1

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
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
nani
  • 183
  • 2
  • 15
  • 1
    Just write a custom Comparator and use Collections.sort. By this you will still be able to sort the way you want ... Refer : http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – StackFlowed Jan 20 '17 at 16:00
  • 2
    `Collections.sort((a, b) => a.getPrename().compareTo(b.getPrename()))` – Mr. Polywhirl Jan 20 '17 at 16:04
  • 2
    Lets say the algorithm is at Dan now. Is `Dan Prename-comparison-with-Dennis > 0`? Yes, write Dan to the list. And the next step would be is `Dan Prename-comparison-with-Nico > 0`? Yes, add Dan to the list. That is how you get multiple names. – DuKes0mE Jan 20 '17 at 16:04
  • Use a OrderedSet to avoid the problem you are having. – StackFlowed Jan 20 '17 at 16:05
  • 1
    @StackFlowed : there is no OrderedSet je JRE. Have you meant TreeSet? – rkosegi Jan 20 '17 at 16:07
  • Possible duplicate of [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – Grzegorz Górkiewicz Jan 20 '17 at 17:22

1 Answers1

1

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
}
Community
  • 1
  • 1
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132