Hi,
I have class of student object with the String student name and double GPA they are stored in array. My GUI have button for sort by name and sort by grade. I want to sort the name and the grade and I want to sort it using Comparator, but I don't know what is Comparator and when to use it. Also what is the diffrnet between Comparator and Comparable.

Comparator is use to sort the collections. Since you have an array, u first need to convert Array into an ArrayList<Student> in generics form.
After that you need to implement Comparator interface.

Please look at below code:

Person.java

package test;

public class Person {

    private int age;
    private String firstName;
    private String secondName;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getSecondName() {
        return secondName;
    }
    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }
}




package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ComparatorImpl {

    public static void main(String args[]) {
        Person person = new Person();
        person.setAge(25);
        person.setFirstName("Kumar");
        person.setSecondName("Bhatia");

        Person personOne = new Person();
        personOne.setAge(35);
        personOne.setFirstName("Ashish");
        personOne.setSecondName("Bhambhani");

        Person personTwo = new Person();
        personTwo.setAge(45);
        personTwo.setFirstName("Sameer");
        personTwo.setSecondName("Kulkarni");

        List<Person> persons = new ArrayList<Person>();
        persons.add(person);
        persons.add(personOne);
        persons.add(personTwo);

        Collections.sort(persons, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                // TODO Auto-generated method stub
                return ((Person) o1).getFirstName().compareTo(
                        ((Person) o2).getFirstName());
            }

        });

        for (Person person2 : persons) {
            System.out.println(person2.getFirstName());
        }
    }

}

For difference betweeen Comparator and Comparable you can visit my blog on java
(http://www.javacodeimpl.blogspot.com)

[you] first need to convert Array into an ArrayList<Student>

No, he doesn't. Arrays can be sorted using Arrays.sort, which works just like Collections.sort, but for arrays.

@kumar.bhatia18 thank you for your help it worked for me when I used the Array instade of arrayList

I have another question I'm trying to sort integer and in one of the website I found this

the only thing that I don't understand in this code is what does the 1 0 and -1 and how does this code work

static class IntComparator implements Comparator<Person> {

public int compare(Person o1, Person o2) {
int i = o1.index;
int j = o2.index;

if (i < j) {
return -1;
} else if (i == j) {
return 0;
} else {
return 1;
}
}

}

The way that compare works is that it returns a negative number if the first argument is less than the second, a positive number if the first argument is greater than the second and 0 if they're equal. So return -1 means "o1 is less than o2", return 0 means "o1 is equal to o2" and return 1 means "o1 is greater than o2".

compare

The way that compare works is that it returns a negative number if the first argument is less than the second, a positive number if the first argument is greater than the second and 0 if they're equal. So return -1 means "o1 is less than o2", return 0 means "o1 is equal to o2" and return 1 means "o1 is greater than o2".

but why did we chose 1 -1 and 0 does it mean that if o1 is greater than o2 then put o1 at the top

The normal sort is an ascending sort, the lesser objects sort before the greater, so is o2 is greater than o1 it will sort after it. Just think of numbers - you expect them to sort 1,2,3,4,5... smaller/lesser first, greater/larger after

This article has been dead for over six months. Start a new discussion instead.