Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing compareTo

 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String also implements the Comparable interface, so if you are happy with the order that Strings are sorted, you could just do
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic