==>java Provide comparator and Coprator interface. this interface used to sort a data.
Here i used Comparator Interface to Sort A Month Name.
==>This Interface is very useful when ever i want to Compare two String Object .
Main.xmlfile
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.college.sortmonthname.MainActivity">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="monthname"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.college.sortmonthname;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView) findViewById(R.id.tv_name);
List<String> month = Arrays.asList("Feb", "Jul", "Jan", "Apr", "Dec",
"Aug", "Oct", "May", "Sep", "Nov", "Jun", "Mar");
final Comparator<String> compare = new Comparator<String>() {
public int compare(String o1, String o2) {
SimpleDateFormat sdate = new SimpleDateFormat("MMM");
Date s1 = null;
Date s2 = null;
try {
s1 = sdate.parse(o1);
s2 = sdate.parse(o2);
} catch (ParseException e) {
e.printStackTrace();
}
return s1.compareTo(s2);
}
};
Collections.sort(month, compare);
name.setText(String.valueOf(month));
}
}
output
Here i used Comparator Interface to Sort A Month Name.
==>This Interface is very useful when ever i want to Compare two String Object .
Main.xmlfile
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.college.sortmonthname.MainActivity">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="monthname"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.college.sortmonthname;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView) findViewById(R.id.tv_name);
List<String> month = Arrays.asList("Feb", "Jul", "Jan", "Apr", "Dec",
"Aug", "Oct", "May", "Sep", "Nov", "Jun", "Mar");
final Comparator<String> compare = new Comparator<String>() {
public int compare(String o1, String o2) {
SimpleDateFormat sdate = new SimpleDateFormat("MMM");
Date s1 = null;
Date s2 = null;
try {
s1 = sdate.parse(o1);
s2 = sdate.parse(o2);
} catch (ParseException e) {
e.printStackTrace();
}
return s1.compareTo(s2);
}
};
Collections.sort(month, compare);
name.setText(String.valueOf(month));
}
}
output