0

My code is as follows:

Person.java

package com.example;

import java.util.Comparator;

public class Person implements Comparator<Person> {

    String name;
    int emp_id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getEmp_id() {
        return emp_id;
    }

    public void setEmp_id(int emp_id) {
        this.emp_id = emp_id;
    }

    public Person(String name,int emp_id) {
        this.name = name;
        this.emp_id = emp_id;
    }
}

PersonSort.java

package com.example;

import com.example.Person;
import java.util.*;

public class PersonSort implements Comparator<Person> {

    public int compare(Person arg0, Person arg1) {
        return arg0.getName().compareTo(arg1.getName());
    }
}

PersonDetails.java

package com.example;

import java.util.*;

public class PersonDetails {

    public List<Person> enter() {
        List<Person> list = new ArrayList<Person>();
        list.add(new Person("D", 1));
        list.add(new Person("A", 56));
        list.add(new Person("G", 43));
        list.add(new Person("C", 32));
        return list;    
    }

    public static void main(String[] args) {
        PersonDetails details = new PersonDetails();
        List<Person> list = details.enter();
        Collections.sort(list, new PersonSort());
        Iterator<Person> it = list.iterator();
        while (it.hasNext()) {
            Person p = it.next();
            System.out.println(p.getName() + " " + p.getEmp_id());
        }
    }
}

In PersonSort.java we are using compareTo to compare two strings. Is there any alternate way like what we do when we compare two integers? For instance below to compare two employee ids:

public int compare(Person arg0, Person arg1) {
    if (arg0.emp_id == arg1.emp_id)
        return 0;
    else if (arg0.emp_id > arg1.emp_id)
        return 1;
    else
        return -1;
}

Can there be any logic like this to compare two strings without calling compareTo?

Andrew Swan
  • 13,427
  • 22
  • 69
  • 98
user728907
  • 33
  • 6
  • 1
    And why on earth would you want to do something like that? And even if it was possible, isn't using `compareTo` easier than writing the comparison logic on your own? – Rohit Jain Feb 13 '13 at 09:51
  • 1
    Try to know the difference first in here : http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html and here :: http://stackoverflow.com/questions/1440134/java-what-is-the-difference-between-implementing-comparable-and-comparator – The Dark Knight Feb 13 '13 at 09:54
  • 1
    You cannot use `==` to compare objects. It will just compare the objects reference and not the actual value. Only for primitive variables you can use `==` to compare its value. – basiljames Feb 13 '13 at 10:00
  • Best article about this in [java tutorial](http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html) – bsiamionau Feb 13 '13 at 10:03
  • A possible duplicate, but go through the below links. [1]: http://stackoverflow.com/questions/420223/what-is-the-difference-between-compare-and-compareto [2]: http://stackoverflow.com/questions/1440134/java-what-is-the-difference-between-implementing-comparable-and-comparator – aksappy Feb 13 '13 at 10:04
  • 1
    Your title doesn't seem to match your question; the code makes no mention of Comparable. Is Person meant to implement Comparable (which is what I'd expect) instead of Comparator (which PersonSort already implements)? – Andrew Swan Feb 13 '13 at 10:10

1 Answers1

0

Can there be any logic like this to compare two strings without calling compareTo?

Well you could write some code did the same thing as String.compareTo. (And there's no mystery what it does: look at the OpenJDK source code for instance.)

But there is no point in doing this. The existing String method does it faster than your custom code could possibly do because the String method can directly access the private state of the string objects.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216