0

I need to take a collection of objects using the CompareTo() command, and then have these stored in a list, and then use the collections.sort() command to sort them alphabetically by last name, then by first name if the last name isn't strong enough, and then print off the entire list at the end.

This is the code I have so far:

package sortlab;
import java.io.*;
import java.util.*;
public class SortLab {
    public static void main(String[] args) throws Exception {
       File youSaidUseOurRelativeFileNameForStudentData = 
            new File("C:/My192/SortLabProj/src/sortlab/student.data");
       Scanner sc = new Scanner(youSaidUseOurRelativeFileNameForStudentData);        
       ArrayList<Student> StudentList = new ArrayList<Student>();
       while (sc.hasNextLine()) {          
            Student testStudent = new Student(sc.next(), sc.next(), sc.next());
            sc.nextLine();
            StudentList.add(testStudent);
       }
    }

}

And the next class:

package sortlab;
import java.util.*;
class Student implements Comparable<Student> {

    private String first;
    private String last;
    private String address;

    public Student(String f, String l, String a) {
        first = f;
        last = l;
        address = a;
    }

    @Override
    public int compareTo(Student other) {
        if (last.hashCode() > other.last.hashCode()) return 1;
        if (last.hashCode() < other.last.hashCode()) return -1;
        if (first.hashCode() > other.first.hashCode()) return 1;
        if (first.hashCode() < other.first.hashCode()) return -1;
        return 0;
    }

}
3
  • What do you mean by "the last name isn't strong enough"? Commented Jun 27, 2014 at 10:33
  • Comparing hash codes is a very bad idea unless you really want to sort values by their hash code :D. Commented Jun 27, 2014 at 10:35
  • @PeterLawrey: Essentially, if the last names are the same. Sorry for the poor wording :D Commented Jun 27, 2014 at 10:38

2 Answers 2

3

If you want to compare them ASCIIbetically use the String.compareTo method. It would never occur to me to compare hashCodes.

If you want to ignore case, you can use String.compareToIgnoreCase

Sign up to request clarification or add additional context in comments.

2 Comments

This works good. Now, I want to avoid the following when I print a list of students: [sortlab.Student@55f96302, sortlab.Student@3d4eac69, sortlab.Student@42a57993, sortlab.Student@75b84c92, sortlab.Student@6bc7c054, sortlab.Student@232204a1, sortlab.Student@4aa298b, etc.
And if you want to sort locale-sensitivly, then use Collator and CollationKey: docs.oracle.com/javase/8/docs/api/java/text/Collator.html docs.oracle.com/javase/8/docs/api/java/text/CollationKey.html
0

First of all I would add getters for first and last name. Then try this code:

@Override
public int compareTo(Student other) {
    int result = l.compareTo(other.getLastName());
    if (result == 0) {
        return f.compareTo(other.getFirstName());
    } else {
        return result;
    }
}

Then add a toString() method to your Student class:

@Override
public String toString() {
    return f+" "+l+", "+a;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.