java - Order Objects Compare -


this object:

public class trackuserchanges{     private long id;     private long previous; } 

i have arraylist. want sort objects this: - if getprevious() 1 object equal getid object, put first object 1 value on getid().

i have compareto method in class , don't want break that. created method :

public int comparetrack(trackuserchanges o){      if(this.getprevious().equals((o.getid()))){         return -1;     }     ??return this.getprevious().compareto(o.getid());  } 

it's not good. ideas?

example: id,previous

trackuserchanges t = new trackuserchanges ( 2,1) trackuserchanges t1 = new trackuserchanges ( 3,1) trackuserchanges t2 = new trackuserchanges ( 1,1) 

in case t.getprevious() = t2.getid() want order : t2,t,t1

what going not idea.

please read contract compareto():

the implementor must ensure sgn(compare(x, y)) == -sgn(compare(y, x)) x , y. (this implies compare(x, y) must throw exception if , if compare(y, x) throws exception.)

the implementor must ensure relation transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

finally, implementor must ensure compare(x, y)==0 implies sgn(compare(x, z))==sgn(compare(y, z)) z.

if have a(1,2) , b(2,1), not going reflexive: both a.compareto(b) , b.compareto(a) returning same sign, violate first rule.

you haven't thought of how handle comparation between a(1,2) , b(3,4). doubt if can make transitive , reflexive.

tell why need , there better choices.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -