1

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
MobileCrysis
  • 31
  • 1
  • 3
  • 1
    You implement the compare function (necessary for comparator interface), so that Java knows how to compare two objects in your list. Java otherwise has no idea when one OBJECT is greater or less than another object. Then you just let it loose on the list. It's very powerful. – kermit Nov 25 '12 at 05:19
  • Why don't you store the student ID as an integer i.e. without the U then append the U to the ID in another class property with a getter which appends the U to the student ID. – Mark Nov 25 '12 at 05:31
  • possible duplicate of [How to sort an arraylist of objects using one common property](http://stackoverflow.com/questions/13354918/how-to-sort-an-arraylist-of-objects-using-one-common-property) – Donal Fellows Nov 25 '12 at 15:33

4 Answers4

3

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:

nair.ashvin
  • 791
  • 3
  • 11
1

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();
}

}
user1432233
  • 63
  • 1
  • 10
Harsha
  • 505
  • 5
  • 11
0

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.

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
0

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.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73