我有一点问题,我知道我可以使用图书馆的排序功能。 但这是我大学的一项任务,我应该为自己编写一个排序函数。 所以我的名单中有学生对象。
Student(String Prename, String Surname, Int ID, Int gender , String Course)
Student s1 = new Student("Steven", "Schrott", 533546, 1 , "AI");
所以现在我的名单中有4名学生,现在我必须通过Prenames对此列表进行排序。
这是我的排序功能,但它不起作用。
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;
}
我的输出不是这样的,并且我的列表中没有每个Name只有一次,但我的Sort函数有点在新列表中放置它们。
答案 0 :(得分:1)
您可以实施自己的快速排序算法。
这里有一个很好的例子:
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);
}
}
}
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=]
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);
}
}
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());
}
}
public enum Gender {
UNKNOWN,
MALE,
FEMALE
}