java 如何使用 Object 参数对 Arraylist 中的对象进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25311557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort Objects in an Arraylist with an Object parameter
提问by Kebab Programmer
I am having problems with sorting objects in an Arraylist as I am new to sorting object.
我在对 Arraylist 中的对象进行排序时遇到了问题,因为我是排序对象的新手。
Sorting an arraylist is pretty basic, but sorting an arraylist of objects is a completely different matter. Basically I have been going over code here in stack overflow and people seem to use comparators to solve their problems, but they don't explain how to actually call the method and put it into use, and that is why I am here.
对数组列表进行排序是非常基本的,但是对对象的数组列表进行排序是完全不同的事情。基本上我一直在讨论堆栈溢出中的代码,人们似乎使用比较器来解决他们的问题,但他们没有解释如何实际调用该方法并将其投入使用,这就是我在这里的原因。
With the code below I am trying to sort an arraylist of Students with parameters - String name, int age and string course. I then have another class to store Student objects and sort them within the class. Here is what I have :
使用下面的代码,我尝试使用参数对学生数组列表进行排序 - 字符串名称、整数年龄和字符串课程。然后我有另一个类来存储 Student 对象并在类中对它们进行排序。这是我所拥有的:
Student.java
学生.java
public class Student {
private String name, course;
private int age;
public Student(String name, int age, String course) {
this.name = name;
this.age = age;
this.course = course;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getCourse() {
return course;
}
}
CompareObj.java
比较对象.java
import java.util.*;
public class CompareObj implements Comparator<Student>{
private ArrayList<Student> sList;
public CompareObj() {
sList = new ArrayList<Student>();
}
@Override
public int compare(Student s1, Student s2) {
return s2.getName().compareTo(s1.getName());
}
public void add(Student s1) {
sList.add(s1);
}
public void displayList() {
for(int i = 0; i<sList.size();i++) {
System.out.println(sList.get(i).getName());
}
}
}
In CompareObj class, how can I use the implemented method compare(s1,s2), how can I use this method to sort my arraylist of student objects?
在 CompareObj 类中,如何使用实现的方法 compare(s1,s2),如何使用此方法对我的学生对象数组列表进行排序?
回答by Wundwin Born
how can i use this method to sort my arraylist of student objects?
You don't need to call compare()
yourself. You can just pass your comparator to Collections.sort()
that will take care sorting for you by calling compare()
method.
你不需要给compare()
自己打电话。您可以将您的比较器传递给Collections.sort()
它将通过调用compare()
方法为您进行排序。
By using custom class CompareObj
,
通过使用自定义类CompareObj
,
Collections.sort(studentList, new CompareObj());
Or another way without CompareObj
is,
或者另一种方式CompareObj
是,
Collections.sort(studentList,new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getName().compareToIgnoreCase(s2.getName());
}
});
回答by ortis
Your class CompareObj
is mixing both ArrayList
(by encapuslation) and Comparator
(by implenting interface). You don't need that, implementing Comparator
is enough.
你的班级CompareObj
混合了ArrayList
(通过封装)和Comparator
(通过实现接口)。你不需要那个,实施Comparator
就足够了。
Try the following:
请尝试以下操作:
ArrayList<Strudent> students = new ArrayList<Student>();
// fill the ArrayList...
Collections.sort(students, new CompareObj());
This will sort student
by their name, as specified in your CompareObj
class.
这将按student
您的CompareObj
班级中指定的名称排序。
回答by Aniket Thakur
You can use Arrays.sort()
method
你可以使用Arrays.sort()
方法
Arrays.sort(studentArray, new CompareObj ());
However if you think name is a way of naturally ordering Students then make your Student
class implement Comparable
interface and override compareTo()
method.
但是,如果您认为 name 是一种自然排序学生的方式,那么请让您的Student
类实现Comparable
接口和覆盖compareTo()
方法。
回答by Sachindra N. Pandey
Example to sort element in ArrayList
对 ArrayList 中的元素进行排序的示例
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ArrayListSorting {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList al=new ArrayList<>();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
al.add("e");
System.out.println("Before sorting");
System.out.println(al);
Collections.sort(al,new ArrayCustomizedSorting());
System.out.println("After sorting");
System.out.println(al);
}
}
class ArrayCustomizedSorting implements Comparator<String>{
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return -o1.compareTo(o2);
}
}
回答by Beyhan
You can use array sort algorithms for that.
您可以为此使用数组排序算法。
//Convert arraylist to array
ZAATSAYIM[] a = jsonResultWrapper.sayimDTOs.toArray(
new ZAATSAYIM[jsonResultWrapper.sayimDTOs.size()]);
//By date
Date date1, date2;
boolean sorted = false;
ZAATSAYIM temp;
while (!sorted) {
sorted = true;
for (int i = 0; i < a.length - 1; i++) {
date1 = a[i].getCDATETIMEDAte();
date2 = a[i + 1].getCDATETIMEDAte();
int diff = date2.compareTo(date1);
boolean after = (diff > 0);
boolean isSame = (diff == 0);
if (after) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
sorted = false;
} else if (isSame && (a[i].getSignlaStregthInt() < a[i + 1].getSignlaStregthInt())) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
sorted = false;
}
}
}
jsonResultWrapper.sayimDTOs = new ArrayList<ZAATSAYIM>(Arrays.asList(a));