#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    0
    Rep Power
    0

    sorting a Vector


    Hi, i'm kinda new to this but here we go:

    I want to sort a Vector full of instances of a class according to an int declared in the class... How do i do it?

    I've heard about a method called sort() but how do i tell it which variable i want to sort by and asc/dec?

    thankful for help...
  2. #2
  3. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep Power
    0
    It's been a while since I've done this, but as far as I'm aware...
    1) the class should implement comparable
    2) you should overide the compareTo method and have the return as 0 if it's equal a negative number if it's less and a positive number if it's more.

    or something like that

    oscagne
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    0
    Rep Power
    0
    Thanks for the help, but i'm not so sure i follow...

    What do you mean with "override"?

    Can i use this to construct a method that checks every object in the Vector against another and then moves it up or down depending on the compareTo() result?

    I found the sort(object, comparator) method in the java doc, but how do I write a comparator that compares ints in the objects?

    i'm confused man!
  6. #4
  7. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep Power
    0
    Ok,
    I'll write a little class to explain it

    class sortableObject implements Comaparble{

    int sortingvalue;

    public sortableObject(int value){
    this.sortingvalue=value;
    }

    compareTo(Object comparator){
    ((Integer)comparator).intValue()-value;
    }
    }

    Vectors don't use this though, so you might want to use something like a hashtree or something.

    Oh, and to answer your question overriding is defining a method of the same name in a subclass/use of interface with the same return type and arguements. i.e. Comparable defines compareTo(Object), but you are defining a new method for it

    Is that any help?

    oscagne
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    0
    Rep Power
    0
    thanks for the code, really sweet of you!

    In this segment...

    compareTo(Object comparator){
    ((Integer)comparator).intValue()-value;
    }

    ...what is Object comparator and (Integer)comparator? Are those statements meaning ">", "<" or "=" but written in code?

    But you say I can't use this with a Vector? I'm doing a school assignment and they suggested that i'd use a Vector, so I guess there has to be some way to do it...

    Anyway I'll read more about it tomorrow, good night and thanks again!
  10. #6
  11. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep Power
    0
    Hi,

    Seeing as you project is about vectors then theres probably no reason to go into what I was doing in that compareTo method, but I will anyway... the compareTo method that is in the comparable interface takes an object arguement, and to properly implement this interface you need to override that method, so you have to use the same type (i.e. Object). But you want to compare integers so you have to use the Integer object which is in effect a wrapper for an int (i.e. the one you want to compare). So what you need to do to get the correct return value is cast the object (comparator) into an Integer object which is done by using the "(Integer)", and then get the int out of it by calling Integers intValue() method. you can then compare it to the value in the that instance. But as I said before Vectors aren't sorted, so this is kinda pointless really. So if you've got to this point ignore the above

    It sounds like, if you have to use a Vector and you have to use a sort method then you will have to create a class that extends Vector and create your own sort method, if you did this then you could probably use an altered version of the above, in which you pass in the whole object into the arguement and cast it to an object of it's own type, then either use make the global int that you are comparing public, or make a method to return it so that you can sort it.

    so your version of the vector class would be

    class myVector extends java.util.Vector{
    int ASC=1, DESC=2;
    sort(int direction){
    for(int i=0;i<this.size()-1;i++){
    for (int j=i;j<this.size()-1;j++){
    if (direction==ASC){
    if(get(j).compareTo(j+1)<1){
    sortableObject temp1=(sortableObject)get(j)
    sortableObject temp2=(sortableObject)get(j+1);
    put(j,temp2);
    put(j+1,temp1);
    }
    }
    else{
    if(get(j).compareTo(j+1)>1){
    sortableObject temp1=(sortableObject)get(j)
    sortableObject temp2=(sortableObject)get(j+1);
    put(j,temp2);
    put(j+1,temp1);
    }
    }
    }
    }
    }
    }

    and you change the compareTo method in the sortableObject to

    int compareTo(Object comapator){
    return ((sortableObject)comparator).getValue()-value;
    }

    and add the method

    int getValue(){
    return sortingvalue;
    }

    Is this any good?

    Sorry people...possibly the longest post in the universe :-)

    Oscagne
  12. #7
  13. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep Power
    0
    Ok,
    erhum, it was late when I typed out that code, and I was bound to get some of it wrong. I've done it again now and tested it. This one works!! I probably completely misread what you wanted, but this is how I'd do it. (follow 3 classes, one of which is a tester class). If they aren't readable. I'll attach the files aswell

    import java.util.Random;

    public class Tester{

    public static void main(String args[]){
    myVector v1=new myVector();

    Random r= new Random(20000);

    for (int i=0;i<100;i++){
    int randomnum=Math.abs(r.nextInt());
    sortableObject s1 = new sortableObject(randomnum);
    v1.add(s1);
    }

    v1.sort(1);
    System.out.println("go");
    for (int i=0;i<v1.size();i++){
    System.out.println(((sortableObject)v1.get(i)).getValue());
    }
    }
    }

    class myVector extends java.util.Vector{

    int ASC=1, DESC=2;

    public void sort(int direction){
    for(int i=0;i<size()-1;i++){
    if (direction==ASC){
    for (int j=i;j<size()-1;j++){
    if(((sortableObject)get(j)).compareTo((sortableObject)get(j+1))<0){
    sortableObject temp1=(sortableObject)get(j);
    sortableObject temp2=(sortableObject)get(j+1);
    set(j,temp2);
    set(j+1,temp1);
    }
    }
    }
    else{
    for (int j=i;j<size()-1;j++){
    if(((sortableObject)get(j)).compareTo((sortableObject)get(j+1))>0){
    sortableObject temp1=(sortableObject)get(j);
    sortableObject temp2=(sortableObject)get(j+1);
    set(j,temp2);
    set(j+1,temp1);
    }
    }
    }
    }
    }
    }

    class sortableObject implements Comparable{

    int sortingvalue;

    public sortableObject(int value){
    this.sortingvalue=value;
    }

    public int compareTo(Object comparator){
    return ((sortableObject)comparator).getValue()-sortingvalue;
    }

    public int getValue(){
    return sortingvalue;
    }

    }

    Oscagne
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    0
    Rep Power
    0
    Thanks a lot, this is great!

    I'll try to understand the code and then see if I can implement it into my assignment...

    I'm a little bit confused by the class myVector, is that a "trick" to be able to write methods for vectors (extends Vector...)?

    Anyway, thank you for your time and effort!

    /danko
  16. #9
  17. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep Power
    0
    yeah,

    creating myVector, is a way of creating a vector with more features. It includes all the original features, so that you can use sort(), get(), set(), etc. But now you add a sort() method, which it didn't have before. This is as you say "extending" Vector, but can also be called inheititing from Vector, or being a subclass of Vector

    Oscagne
  18. #10
  19. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep Power
    0
    oops, just realised I put sort() as one of the original methods, and obviously it isn't. I'll read my next post before I submit it I think!! :-)

    Oscagne
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    0
    Rep Power
    0
    Hey Oscagne, whats up?

    I made your sorting engine work with my little program, i'm really grateful!

    On the other hand: it seems like approx. only the second half of the objects gets sorted, the first half is kinda random...

    There has to be some kind of bug in the sort() method, but i'm not able to locate it, can you?

    keep in touch/danko
  22. #12
  23. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep Power
    0
    Hi,

    Yeah, you're completely right, my code wasn't exactly right. I checked it by looking at the stuff at the end, which seemed fine, but the rest wasn't. Check below for the way myVector should have looked (also attached).

    But first, Vector doesn't have a sort method. It is meant not to be sorted, it is meant purely as a dyamically resizeable data structure. Other java data structures such as TreeMap are sorted and use the Comparable interface to determine the order. I took what you said about your assignment to mean creating an example of a sortable Vector, but maybe you are meant to create a method to sort a vector (i.e. a method that takes an unsorted Vector as a parameter and returns a sorted Vector). If it was then you can use a similar method to my sort method to achieve it, and yes I know it's not a particularly nice algorithm but it works (NOW :-) ). Anyway, the code below is how it should have been...

    class myVector extends java.util.Vector{

    int ASC=1, DESC=2;

    public void sort(int direction){
    for(int i=1;i<size();i++){
    if (direction==ASC){
    for (int j=0;j<size()-i;j++){
    if(((sortableObject)get(j)).compareTo((sortableObject)get(j+1))<0){
    sortableObject temp1=(sortableObject)get(j);
    sortableObject temp2=(sortableObject)get(j+1);
    set(j,temp2);
    set(j+1,temp1);
    }
    }
    }
    else{
    for (int j=0;j<size()-i;j++){
    if(((sortableObject)get(j)).compareTo((sortableObject)get(j+1))>0){
    sortableObject temp1=(sortableObject)get(j);
    sortableObject temp2=(sortableObject)get(j+1);
    set(j,temp2);
    set(j+1,temp1);
    }
    }
    }
    }
    }
    }
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    0
    Rep Power
    0
    thanks a lot, you saved me!

    i'll put your name in the comments!

    /danko

IMN logo majestic logo threadwatch logo seochat tools logo