views:

60

answers:

4

Hello i want to make a sort in java. In my object i have many element so i want to make this sort with power and model

public class Product implements Comparable<Product>,Serializable
{   
private int idProduct ;
private int power;
private String model;
private String  color;
[...]
@Override
    public int compareTo(Product o) {
        return String.valueOf(this.power).compareTo(String.valueOf(o.power));

    }

so how to make sort with power and model

+1  A: 

The following will first sort on power and in case of a tie, will sort on model:

public int compareTo(Product that) {
  if(this.power != that.power) {
    return this.power < that.power ? -1 : 1;
  } 
  else {
    return this.model.compareTo(that.model);
  }
}

Or if you need to compare power lexicographically, do:

public int compareTo(Product that) {
  if(this.power != that.power) {
    return String.valueOf(this.power).compareTo(String.valueOf(that.power));
  } 
  else {
    return this.model.compareTo(that.model);
  }
}
Bart Kiers
nice, except `o` and `that` aren't the same word you know :P
gnud
Woops! Thanks gnud. :)
Bart Kiers
+1  A: 

You need to decide which is the first thing to sort by, and which is the second. Once you decide that, you simply implement the compareTo method like so:

@Override
public int compareTo(Product o) {
    int result = this.model.compareTo(o.model);
    if (result == 0)
        return String.valueOf(this.power).compareTo(String.valueOf(o.power));
    else
        return result;
}

Or, if you don't want to change the compareTo method, you can write your own Comparator like so:

public class ProductComparator implements Comparator<Product> {
    public int compare(Product p1, Product p2) {
        int result = p1.model.compareTo(p2.model);
        if (result == 0)
            return String.valueOf(p1.power).compareTo(String.valueOf(p2.power));
        else
            return result;
    }
}
jjnguy
It's quicker but it gives a different sort order (e.g. 2 < 11 but "2" > "11")
Simon Nickerson
@Simon, Oh, good call, I didn't consider that...ty!
jjnguy
Why convert power to a String in order to compare? You could just write: result = this.power - o.power.
Adamski
@Adamski look at @Simon's comment to my answer.
jjnguy
A: 

CompareTo should return -1 when this is less than o, 0 when they are equal, and 1 when this is greater than o.

Presumably you're aiming to use model to sort the items with the same power, so you'd do something like:

int powerDiff=String.valueOf(this.power).compareTo(String.valueOf(o.power));  
if(powerDiff==0)  
  return String.valueOf(this.model).compareTo(String.valueOf(o.model));  
return powerDiff;
Jivlain
The contract for compareTo states that it should return a negative integer, 0 or a positive integer depending on the outcome (e.g. it doesn't have to return -1, it could return -100).
Adamski
A: 

Some additional points:

1) It is not necessary to convert power to a String in order to compare it. You could do an integer based comparison:

int result = this.power - o.power;
if (result == 0) {
  result = this.model.compareTo(o.model);
}

Note that this will produce a different sort order to the string-based comparison though (e.g. "200" > "10000" whereas 200 < 10000).

2) You could further optimise your compareTo method by checking whether the object is being compared against itself:

public int compareTo(Product that) {
  int ret;

  if (this == that) { // Object is being compared against itself.
    ret = 0;
  } else {
    // Do full comparison.
  }

  return ret;
}

3) From the Comparator Javadoc:

Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map).

Your comparison method is only based on power and model whereas equality will be based on object identity unless you override the equals method. Hence you may want to consider overriding equals (and hashCode) to make them consistent with compareTo.

Adamski