Using Comparator to Sort an Array
It offers an interface , called Comparator that is useful to impose a total ordering on a collection of elements.It can be used to sort the elements of an array into ascending order or descending order.
interface Comparator<T>,where T is generic type
Program Sample:
/**
*
*/
package com.collectionpack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
/**
* @author Abhinaw.Tripathi
*
*/
class Ascending implements Comparator<Integer>
{
@Override
public int compare(Integer o1, Integer o2)
{
return o1.compareTo(o2);
}
}
class Desending implements Comparator<Integer>
{
@Override
public int compare(Integer o1, Integer o2)
{
return o2.compareTo(o1);
}
}
public class ComparatorApp
{
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many elements? ");
int size=Integer.parseInt(br.readLine());
Integer arr[] =new Integer[size];
for(int i=0;i<size;i++)
{
System.out.println("Eneter int: ");
arr[i]=Integer.parseInt(br.readLine());
}
Arrays.sort(arr, new Ascending());
System.out.println("Sorted in asending order");
display(arr);
Arrays.sort(arr,new Desending());
System.out.println("sorted in desending order");
display(arr);
}
private static void display(Integer[] arr)
{
for(Integer i:arr)
System.out.println(i + "\t");
}
}
It offers an interface , called Comparator that is useful to impose a total ordering on a collection of elements.It can be used to sort the elements of an array into ascending order or descending order.
interface Comparator<T>,where T is generic type
Program Sample:
/**
*
*/
package com.collectionpack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
/**
* @author Abhinaw.Tripathi
*
*/
class Ascending implements Comparator<Integer>
{
@Override
public int compare(Integer o1, Integer o2)
{
return o1.compareTo(o2);
}
}
class Desending implements Comparator<Integer>
{
@Override
public int compare(Integer o1, Integer o2)
{
return o2.compareTo(o1);
}
}
public class ComparatorApp
{
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many elements? ");
int size=Integer.parseInt(br.readLine());
Integer arr[] =new Integer[size];
for(int i=0;i<size;i++)
{
System.out.println("Eneter int: ");
arr[i]=Integer.parseInt(br.readLine());
}
Arrays.sort(arr, new Ascending());
System.out.println("Sorted in asending order");
display(arr);
Arrays.sort(arr,new Desending());
System.out.println("sorted in desending order");
display(arr);
}
private static void display(Integer[] arr)
{
for(Integer i:arr)
System.out.println(i + "\t");
}
}
Output:
How many elements?
5
Eneter int:
26
Eneter int:
89
Eneter int:
69
Eneter int:
78
Eneter int:
456
Sorted in asending order
26
69
78
89
456
sorted in desending order
456
89
78
69
26
No comments:
Post a Comment