aspose file tools*
The moose likes Java in General and the fly likes Implementing compareTo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Implementing compareTo" Watch "Implementing compareTo" New topic
Author

Implementing compareTo

Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Hi,

I have this view object,


and I'm populating an ArrayList (I receive data from a recordSet, e.g.,

data = 2007-01-01
regiao = C
valor = 33.3

data = 2007-01-01
regiao = P
valor = 12.3

data = 2007-01-01
regiao = M
valor = 33.3

...

The date is always the same. I want to sort this list accordingly to the regiao variable and the final sorting should be

data = 2007-01-01
regiao = C
valor = 33.3

data = 2007-01-01
regiao = M
valor = 33.3

data = 2007-01-01
regiao = P
valor = 12.3

so, the question is how to write the compareTo to make this kind of sort?

thanks in advance,


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

In the compareTo method:

Look at the value of the 'regiao' variable in the current object and in the object that 'anotherReceita' refers to and compare them.

Return -1 when the regaio of the current object is less than anotherReceita.regaio.

Return 0 when they are the same.

Return 1 when the regaio of the current object is greater than anotherReceita.regaio.

Does this help? If not, can you be more specific about what you don't understand?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
String also implements the Comparable interface, so if you are happy with the order that Strings are sorted, you could just do


Joanne
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
I understand the general way of implementing the compareTo method. My problem is that the sorting is not alphabetically.

My regiao variable receives the possible values: C, A, M, P but the order they get in the List may vary. For example I might receive a List like this

data = 2007-01-01
regiao = C
valor = 33.3

data = 2007-01-01
regiao = P
valor = 12.3

data = 2007-01-01
regiao = M
valor = 33.3

I want my List to become ordered like this

data = 2007-01-01
regiao = C
valor = 33.3

data = 2007-01-01
regiao = M
valor = 33.3

data = 2007-01-01
regiao = P
valor = 12.3

The sorted List must have first the object with regiao=C, then regiao=A (in the previous example I didn't receive the object with regiao=A), then regiao= M and finally regiao = P.
I'm having a hard time to find how to implement this kind of sorting.

thanks,
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
I think you will just have to do a number of comparisons of the two values.
There are a few shortcuts you can use.
e.g.
First check if the values are the same. If so, return 0.
If the value of regiao for the current instance ic C, you can always return 1 as the value in the other instance will make no difference.
If the value of regiao for the current instance ic A, you return -1 if the other value is C, otherwise you return 1
etc

For only four values, it's not that difficult.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
It may also improve performance a bit to get the first character of the String as a char. Comparing chars is probably a bit quicker than comparing Strings. Depends on how many comparisons you are doing as to how much difference it will make.
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by Joanne Neal:
I think you will just have to do a number of comparisons of the two values.
There are a few shortcuts you can use.
e.g.
First check if the values are the same. If so, return 0.
If the value of regiao for the current instance ic C, you can always return 1 as the value in the other instance will make no difference.
If the value of regiao for the current instance ic A, you return -1 if the other value is C, otherwise you return 1
etc

For only four values, it's not that difficult.


That's a possible solution! thanks.
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
What you could do is to create a static and final
ArrayList<String> yourList = new ArrayList<String>(Arrays.asList("C","A","M","P"));

then when you compare your object, use method ArrayList.indexOf(regiao) to obtain a value, ( if you get -1, throw an exception - like unknown regiao code ),

and then compare numbers.
 
 
subject: Implementing compareTo