Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   about sort()-method (http://www.programmingforums.org/showthread.php?t=7188)

säkki Nov 25th, 2005 2:00 PM

about sort()-method
 
Here's the problem little simplified:

I have made two classes Person and CardIndex.

I created Objects from both
Person myP = new Person("FirstName", "LastName");
CardIndex cInd = new CardIndex();

CardIndex is basically a vector where I dump those Persons.

Classes have some other stuff in them too, and Person is a super-class for two other Classes, so in CardIndex-vector can be at least three kind of classes or something like that.
Problem:
I have to sort the cInd by LastName using sort()-method. I'm lost :confused:

//Collections.sort(cInd**!*"*#) brainMalfunction. Thanks for any help.

Arevos Nov 25th, 2005 2:22 PM

You need a custom Comparator to tell Java how to sort. Comparator objects have a "compare" method that takes two Objects as arguments and returns an integer. If the integer is negative, the first object is less than the second; if the integer is positive, the first object is greater than the second; if the integer is 0, the objects are equal. The String.compareTo method can compare two strings in this way.

You can use an anonymous class to save cluttering up your code with a class that will only be used once. Maybe something like:

:

Collections.sort(cInd, new Comparator() {
  int compare(Person a, Person b) {
      return a.getLastName().compareTo(b.getLastName());
  }
});


xavier Nov 25th, 2005 2:30 PM

Shouldn't that be :
:

Collections.sort(cInd, new Comparator() {
  int compare(Object o1, Object o2) {
      Person a = (Person)o1;
      Person b = (Person)o2;
      return a.getLastName().compareTo(b.getLastName());
  }
});


Arevos Nov 25th, 2005 2:50 PM

Yes, you're right. Though, if you have Java 1.5:
:

Collections.sort(cInd, new Comparator<Person>() {
  int compare(Person a, Person b) {
      return a.getLastName().compareTo(b.getLastName());
  }
});

(Assuming cInd is descended from List<Person>)

säkki Nov 25th, 2005 3:17 PM

Quote:

Originally Posted by Arevos
Yes, you're right. Though, if you have Java 1.5:
:

Collections.sort(cInd, new Comparator<Person>() {
  int compare(Person a, Person b) {
      return a.getLastName().compareTo(b.getLastName());
  }
});

(Assuming cInd is descended from List<Person>)

well it actually isn't descended from List<Person>, if I understand what you mean. cInd contains three kind of "persons" could be like this:
cInd {Person("FN", "LN"), Student("FN", "LN"), Worker("FN", "LN")} <- not meant to be anykind of syntax :) just to clarify things.
Student and Worker are descended from Person.

Thanks for these tips allready, I'll try to get forward in this but I really am miserable in this.

Arevos Nov 25th, 2005 4:02 PM

Quote:

Originally Posted by säkki
well it actually isn't descended from List<Person>, if I understand what you mean. cInd contains three kind of "persons" could be like this:
cInd {Person("FN", "LN"), Student("FN", "LN"), Worker("FN", "LN")} <- not meant to be anykind of syntax :) just to clarify things.
Student and Worker are descended from Person.

Java 1.5 has a new feature called generics, which, amongst other things, allows you you to tie a type to a container

Before Java 1.5, you had to cast an element as the correct type when removing it from a container:
:

Vector list = new Vector();
list.add("Hello");
list.add("World");
String lastElement = (String)list.get(0);

In Java 1.5, you can use generics to tell the JVM which type to expect from a container:
:

Vector<String> list = new Vector<String>();
list.add("Hello");
list.add("World");
String lastElement = list.get(0);

The class Vector<Person> will take Persons, and descedants of Persons (ie. Workers and Students).
:

Vector<Person> persons = new Vector<Person>();

persons.add(new Person("A", "B"));
persons.add(new Worker("A", "B"));
persons.add(new Student("A", "B"));

for (Person person : persons) {
  System.out.println(person);
}

Of course, if you're not using Java 1.5, you can safely ignore all this talk about generics.

Quote:

Originally Posted by säkki
Thanks for these tips allready, I'll try to get forward in this but I really am miserable in this.

You seem to be doing okay so far :)

säkki Nov 27th, 2005 3:01 PM

OK thank you guys, finally got it working. Had some problems with the whole idea (what to do in which class etc). Post my solution here if it may help someone else.

:

In class Person:
//implements Comparable
-----------------------------
public int compareTo(Object next) {
        String bLastN = ((Person)next.getLastName();
        int comp = ((Person)this).getLastName().compareTo(bLastN);         
        if (comp == 0) return 0;    //guess this is the only return statement that is
        if (comp < 0) return -1;  //needed, but the 2 other doesn't seem to do
        else return 1;          //any harm either
}
------------------------------------------

In class CardIndex:
public void sortItorScrewIt(){
          Collections.sort(cInd);
}



All times are GMT -5. The time now is 11:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC