简体   繁体   English

如何根据一个属性对对象的arraylist进行排序?

[英]How to sort an arraylist of objects based on one property?

I have an arrayList of Student objects. 我有一个Student对象的arrayList。 The objects all have common properties such as first and last name, UID (university ID number) GPA, and then there are subclasses for graduate and undergraduate. 这些对象都有共同的属性,如名字和姓氏,UID(大学ID号)GPA,然后有研究生和本科生的子类。 I have been stumped as to how to sort the arraylist based on the UID number in order of lowest to highest. 我一直难以理解如何根据UID编号按照从最低到最高的顺序对arraylist进行排序。 The UID number is a STRING in the format of U123456. UID号码是U123456格式的STRING。 There is always a U, and then a varied amount of integers. 总有一个U,然后是不同数量的整数。 Because of the U, I cannot parse into an int and sort that way, I have seen the Comparator class being used, but I do not understand it, as it seems to only compare two objects?! 因为U,我无法解析为int并按此方式排序,我已经看到使用了Comparator类,但我不明白它,因为它似乎只比较两个对象?! IF the Comparator class will work for this, could someone please explain to me what it does and how it works? 如果Comparator课程适用于此,有人可以向我解释它的作用和工作原理吗?

It is easy to compare because of this behaviour: 由于这种行为,很容易比较:

System.out.println("u1".compareTo("u2")); // -1
System.out.println("u2".compareTo("u2")); // 0
System.out.println("u3".compareTo("u2")); // 1
System.out.println("u4".compareTo("u2")); // 2

So this is what you need to do: 所以这就是你需要做的:

You can also do with Comparer class as you have already mentioned. 您也可以使用Comparer类,如您所述。

If you implement Comparator class from the Student class, you need to provide the implementation for the compare method where you can specify your logics. 如果从Student类实现Comparator类,则需要提供compare方法的实现,您可以在其中指定逻辑。 This method will be called when you execute Collections.sort(list, comparator) method. 执行Collections.sort(list,comparator)方法时将调用此方法。 Further you can put debug points in compare method just to make sure how it is being called. 此外,您可以将调试点放在比较方法中,以确保调用它的方式。

Complete example for your problem is as follows : 您的问题的完整示例如下:

import java.util.Comparator;
public class Student implements Comparator<Student>{

private String uid;

@Override
public int compare(Student o1, Student o2) {

Integer i1 =  Integer.parseInt(o1.getUid().substring(1)); // Skip the first character U
Integer i2 = Integer.parseInt(o2.getUid().substring(1));  // Skip the first character U
if(i1 > i2 ){
    return 1;
}else if(i1< i2){
    return -1;
}else {
    return 0;
}
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

}



package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSort {

public TestSort(){

Student s1 = new Student();
s1.setUid("U34334");

Student s2 = new Student();
s2.setUid("U64454");

Student s3 = new Student();
s3.setUid("U13344");

List<Student> list = new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);


Collections.sort(list, new Student());

for(Student s : list){
    System.out.println(s.getUid());
}

}

public static void main(String[] args){
new TestSort();
}

}

Try this : 尝试这个 :

ArrayList lsStudent = GetStudentData();

lsStudent.Sort((S1, S1) => Convert.ToInt32(S1.UID.Substring(1))
                          .CompareTo(Convert.ToInt32(S2.UID.Substring(1))));

This will fix your problem. 这将解决您的问题。

Make your Student class to implement Comparable Interface: 让您的Student类实现Comparable Interface:

     public class Student implements Comparable<Student> {

and implement compareTo method in your Student class as below: 并在您的Student类中实现compareTo方法,如下所示:

@Override
public int compareTo(Student s) {
   return UID.compareToIgnoreCase(s.UID);
}

Here UID.compareToIgnoreCase(s.UID) will take care of your alphanumeric value comparison for sorting purpose. 这里UID.compareToIgnoreCase(s.UID)将处理您的字母数字值比较以进行排序。

Then simply use Collections.sort to sort your student object collection. 然后只需使用Collections.sort对学生对象集合进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM