Sorting Example Using Comparable,Comparator Interface in android

This exmpale will show you how to sort data in android .

-This example include sort data on date and string both.
-This example will show you how to use both these interfaces.

Interfaces
//Comarator<//your model name>
//Comparable<//your model name>

Create a model class:-


//class implements comparable interface to store data
public class Employee implements Comparable<Employee>{

    int id,salary;
    String name,email;
    String date;

    public int getId() {
        return id;
    }

    public int getSalary() {
        return salary;
    }

    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setId(int id) {
        this.id = id;
    }

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

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String emp()
    {
        return "Id : - " + id +"Name :- " + name + "Salary : - " + salary + "Email : - " + email + "Date" + date;
    }

    public  Employee(int id,String name,String email,int salary,String date)
    {

        this.id=id;
        this.name=name;
        this.email=email;
        this.salary=salary;
        this.date=date;

    }

    @Override    public int compareTo(Employee another) {

        if(this.id==another.id){
            return 0;
        }
        else if(this.id>another.id){
            return 1;
        }
        else {
            return -1;
        }

    }


Main.java

public class MainActivity extends AppCompatActivity {

    
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ArrayList<Employee> employees= new ArrayList<Employee>();

        RelativeLayout linear= (RelativeLayout) findViewById(R.id.linearlayout);



        employees.add(new Employee(1,"Mahavir","mahavir_1993@yahoo.com",20000,"21/1/1993"));
        employees.add(new Employee(2,"Akshay","akshay_1993@yahoo.com",30000,"25/1/1997"));
        employees.add(new Employee(3,"Rahul","rahul_1993@yahoo.com",10000,"28/1/2011"));
        employees.add(new Employee(4,"Sahin","sahin_1993@yahoo.com",90000,"12/1/2005"));
        employees.add(new Employee(5, "Vivek", "vivek_1993@yahoo.com", 80000, "18/1/2008"));
        employees.add(new Employee(6, "Rajesh", "Rajesh_1993@yahoo.com", 81000, "20/1/2010"));



        Collections.sort(employees, new DateComparator());

        //Collections.sort(employees,new Name()); 
        TextView tv=new TextView(MainActivity.this);



        for(int i=0;i<employees.size();i++)
        {
            tv.append(employees.get(i).emp() + "\n");
        }


        linear.addView(tv);


    }
}


Create a seprate file and implements the Comparator interface.

   This exmaple will show you sort data to string data type.

public class Name implements Comparator<Employee> {

    @Override    public int compare(Employee lhs, Employee rhs) {


        Log.i("ID lhs", "" + lhs.getId());
        Log.i("ID rhs", "" + rhs.getId());


//
        return lhs.getName().compareToIgnoreCase(rhs.getName());



    }
}

Create a seprate file and implements the Comparator interface.

   This exmaple will show you sort data to date data type.

public class DateComparator implements Comparator<Employee> {


    @Override    public int compare(Employee lhs, Employee rhs) {


        SimpleDateFormat format=new SimpleDateFormat("dd/mm/yyyy");
        Date date1=null,date2 = null;

        Log.i("Date 1", "" + date1);
        Log.i("Date 2", "" + date2);

        try {

            date1=format.parse(lhs.getDate());
            date2=format.parse(rhs.getDate());

        } catch (ParseException e) {
            e.printStackTrace();
        }


        return date1.compareTo(date2);
        }
    }




Main.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"   
 xmlns:app="http://schemas.android.com/apk/res-auto"   
 xmlns:tools="http://schemas.android.com/tools"    
android:layout_width="match_parent"   
 android:layout_height="match_parent"   
 android:paddingBottom="@dimen/activity_vertical_margin" 
   android:paddingLeft="@dimen/activity_horizontal_margin"   
 android:paddingRight="@dimen/activity_horizontal_margin"  
  android:paddingTop="@dimen/activity_vertical_margin"   
 app:layout_behavior="@string/appbar_scrolling_view_behavior"  
  tools:context="sortingexample.exampe.com.sortingexample.MainActivity"
    tools:showIn="@layout/activity_main"   
 android:id="@+id/linearlayout">


</RelativeLayout>


Output





Comments