I have a map reduce program that takes input as firstname, lastname and mobile number. I want to group these 3 fields as a single key. For this, I have used `WritableComparable`

interface for a class.

My code is:

```
private static class Multifield implements WritableComparable<Multifield> {
Text firstname1;
Text lastname1;
Text mobile1;
public Multifield(Text firstname1, Text lastname1,Text mobile1) {
this.firstname1 = firstname1;
this.lastname1 = lastname1;
this.mobile1=mobile1;
}
public Multifield() {
this.firstname1 = new Text();
this.lastname1 = new Text();
this.mobile1=new Text();
}
public void write(DataOutput out) throws IOException {
this.firstname1.write(out);
this.lastname1.write(out);
this.mobile1.write(out);
}
public void readFields(DataInput in) throws IOException {
this.firstname1.readFields(in);
this.lastname1.readFields(in);
this.mobile1.readFields(in);
}
public int compareTo(Multifield pop) {
if(firstname1.equals(pop.firstname1))
{
return lastname1.compareTo(pop.lastname1);
}
else
{
return firstname1.compareTo(pop.firstname1);
}
}
@Override
public String toString() {
return "Customer Details [firstname=" + firstname1 + ", lastname=" + lastname1
+ ", mobile=" + mobile1 + "]";
}
```

The above coding just compares firstname and lastname. So, I get the output only based on the first two fields.

**How can I modify the compareTo() method to compare all three fields, so that I can group them together as a key?**

The logic we use for comparing 2 fields is:

```
if(field1 = field2) then Result = 0;
if(field1 > field2) then Result = 1;
if(field1 < field2) then Result = -1;
```

For the sake of explanation, let's assume that, we are comparing 2 objects of same type, containing fields a, b and c.

```
Object 1 contains a1, b1 and c1 (instances of a, b and c)
Object 2 contains a2, b2 and c2 (instances of a, b and c)
```

Following are different cases, we need to handle:

```
a) if(a1 = a2, b1 = b2, c1 = c2) then Result = 0; // (All equal)
b) if(a1 > a2) then Result = 1; // (Since a1 > a2, we short circuit at a)
c) if(a1 < a2) then Result = -1; // (Since a1 < a2, we short circuit at a)
d) if(a1 = a2, b1 > b2) then Result = 1; // (Since b1 > b2, we short circuit at b)
e) if(a1 = a2, b1 < b2) then Result = -1; // (Since b1 < b2, we short circuit at b)
f) if(a1 = a2, b1 = b2, c1 > c2) then Result = 1; // (Since c1 > c2)
g) if(a1 = a2, b1 = b2, c1 < c2) then Result = -1; // (Since c1 < c2)
```

The code for this would be:

```
public int compareTo(Person otherPerson) {
int cmp = firstName.compareTo(otherPerson.firstName);
if(cmp != 0) return cmp;
cmp = lastName.compareTo(otherPerson.lastName);
if(cmp != 0) return cmp;
return mobile.compareTo(otherPerson.mobile);
}
```