MobileCrysis
MobileCrysis

Reputation: 31

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

I have an arrayList of Student objects. 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. I have been stumped as to how to sort the arraylist based on the UID number in order of lowest to highest. The UID number is a STRING in the format of U123456. There is always a U, and then a varied amount of integers. 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?! IF the Comparator class will work for this, could someone please explain to me what it does and how it works?

Upvotes: 1

Views: 9191

Answers (4)

Harsha
Harsha

Reputation: 503

You can also do with Comparer class as you have already mentioned.

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. This method will be called when you execute Collections.sort(list, comparator) method. 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();
}

}

Upvotes: 1

Yogendra Singh
Yogendra Singh

Reputation: 34367

Make your Student class to implement Comparable Interface:

     public class Student implements Comparable<Student> {

and implement compareTo method in your Student class as below:

@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.

Then simply use Collections.sort to sort your student object collection.

Upvotes: 0

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14282

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.

Upvotes: 0

nair.ashvin
nair.ashvin

Reputation: 801

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:

Upvotes: 3

Related Questions