wood burning stoves*
The moose likes Java in General and the fly likes returning comparison counter from collections.sort() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "returning comparison counter from collections.sort()" Watch "returning comparison counter from collections.sort()" New topic
Author

returning comparison counter from collections.sort()

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
hi guys

it seems confusing which forum to choose to post our threads , but no props

(the bellow is only simple demo, i dont think the code works!)
at the current moment, ive got 2 classes MyObjList and MyObj
MyObj implements Obj and Obj is an interface class

public class MyObj implements Obj()

at the moment i sorted the object in more than one order using Collections.sort(ObjList, comp), but what i need to do is to return the number of comparisons that occurs while sorting the list, to do that i need to know how many times has this method called the CompareTo method in MyObj class, i tried to do it in so many ways but ended up with no good!

public class MyObj implements Obj()
{
int compCounter;

public int compareTo(Object o)
{
String anotherObjName = ((Obj) o).getObjName();
compCounter++;
return this.name.compareTo(anotherObjName);
}
}

public class MyObjList implements ObjList
{
//this should return the number of comparisons
public int sortByName()
{
Collections.sort(ObjList, new SortByName());
return compCounter;
}
}

the problem is how do i make sortByName return the compCounter value that is incremented everytime a call is made to compareTo() method? it kept on giving me different errors everytime i try to solve it, e.g. non-static and static problems!
hope u know what i mean.. and i will really appriciate any help
i can sport little logical errors in the code i demostrated, but dont worry about it!
malleat
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
This should work


This will only work if you are only sorting MyObj objects. If it were me I would probably put the static counter in the Comparator you are using, but the above code 'should' work.
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
hello
thanks for that, but its exactly what i have in my original code, ive got a method called
public int getCompCount()
{
return compCounter;
}

but since Obj class is an interface class and MyObjList cant contact MyObj class directly or something like that, as i far as i know!
when i did similer to ur code before i got this error

ERROR: non-static method getCompCount() cannot be refrenced from a static context

how do i make the counter static? and thanks alot for attempting to help anyway
malleat
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Since compCounter is static, getCompCounter() must be static in order to access it. However, I second Steven's recommendation to put the counter and comparison method in a Comparator because you don't want to access MyObj directly.Note that in the call to sort() you were passing in the interface ObjList which clearly won't work. If MyObjList isn't a List implementation, then replace "this" above with the reference to the actual List (probably an instance member of MyObjList).

[ Renamed comparator by removing "My" prefix. ]
[ January 05, 2005: Message edited by: David Harkness ]
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
hello

thanks David, i may not have mentioned that getCompCount() is overriden in MyObj class, meaning that its not inside the comparator class, and the other thing is that my comparators are all inner classes, and i wrote them in MyObjList class...

i know i should put the name of the class where getCompCount() is (which is MyObj) but MyObj implements the interface class Obj, and so i cant access it directly, i tried to put Obj instead of comp (bellow) but its not doing it, as i said it gave me that error in my 2nd reply, i tried something like public Obj comp; but also didnt make it any better, actually the method stopped doing the comparison!!

return comp.getCompCounter();

u wrote
(((Since compCounter is static, getCompCounter() must be static in order to access it))) how do i make it static? i cant change the signiture if its something to do with that?

thanks
malleat1
[ January 06, 2005: Message edited by: H Melua ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by H Melua:
u wrote
(((Since compCounter is static, getCompCounter() must be static in order to access it)))
Wow, I did say that -- what was I thinking? It's backwards. If a field is an instance member, then static methods cannot access it, but that doesn't apply here.

Another possibility is to change compCounter to an instance field so each MyObj tracks how many times it was compared against another MyObj. Then, after sorting, loop through the list of MyObjs and sum up their counters.

Short of a redesign, I don't see another option. Are all of these requirements set in stone?
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
helllooo

ahhh i cant wait till this is done, its taken so much time from me!

Another possibility is to change compCounter to an instance field

such an easy terminology!! how can i make it instance field? and what is it at this moment ?

Are all of these requirements set in stone?

yes they are, the interface class is given to us!!! not very helpful anyway!!! i can guess an easy way of doing it rather than making a call to the method, i could just return the compCounter, but that will only work for 3sorting orders but not the forth 1, plus its not very perfessional and we're expected to make use of it thats y its there, so i know its not to be done that way ... hope i dont give up :roll: !

thanks so much for ur help
malleat1
[ January 07, 2005: Message edited by: H Melua ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Could you post the code you have been given?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by H Melua:
such an easy terminology
Sorry, that was vague. All members (fields and methods) are either instance or static members. As we've discussed, static members belong to the class itself (the single Class object in the JVM) while instance members belong to the individual instances of that class (the objects).

Thus, if you remove the static keyword from before compCounter, it means that each MyObj instance/object will have its own counter.

And yes, please post the code you were given.
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
hi
ok this is the important code, surely i cant put all the code, i'll be here for ever copying and pasting , the code is perfectly woking, and all the methods in the interfaces are written (i deleted the once u dont need to c)...



thats MyObj class



thats the ObjList interface bellow


and this is MyObjList interface


thats all the code u need, u surely dont need to c the applet and other helper classes!!
thanks to u all, i wish im yet a perfect programmer
Hannah
[ January 08, 2005: Message edited by: H Melua ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Excellent! That helps a ton. From the code you posted, if the requiement was to only sort by one method, then I'd say it's pretty straight-forward.
  • As compCounter is non-static, each MyObj tracks how many times it compared itself to another MyObj.
  • All compCounters start at 0.
  • Collections.sort() calls compareTo on various MyObjs in the list.
  • Now your sort method loops over all the MyObjs in the list and adds each one's cmopCounter to a running total and returns that total.

  • Basically, you sort all the objects and then ask each one how many times it compared itself to another. Add them up to get the total.

    Since MyObj.compareTo() compares IDs, this will work for sortByID().To get that to work when sorting using your Comparators, add a call to compareTo() on one of the Objs before doing the real comparison. This is definitely a kludge, and could be made nice with a method on Obj: incCompCounter(). However, you're only "loss" is all the extra comparisons by ID that occur when sorting by other fields.The loop to total the compCounters above (hint: break the loop out into a new method for all the sort methods to share) will work exactly the same when used with this Comparator.

    Good luck!
    H Melua
    Ranch Hand

    Joined: Jan 04, 2005
    Posts: 172
    im so happy

    i cant believe it worked, now i can sit down and work on the documentation although today is the last day in my xmas holidays , hope i'll have time

    but i dont really understand y do we need to make another call to compareTo() (obj1.compareTo(obj2); ) in the comparators??? what does it exactly do? i mean we are calling it anyway here(return obj1.getName().compareTo(obj2.getName()); ) !! do u understand what im pointing at?

    ohh god may i ask u to help this person whenever he remembers u and make him just as happy as he made me, and surely more... amen, and thanks to u steven, and all others who had a look at it inorder to find a solution but couldnt...


    [ January 09, 2005: Message edited by: H Melua ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: returning comparison counter from collections.sort()
     
    Similar Threads
    PriorityQueue ,add and compare object
    Sorting ArrayList of Hashtables
    Sorting the String Saved in ArrayList
    Sorting with Collator and Collections ??
    comparable and comparator