• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing compareTo

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic