import static java.lang.System.out;
import java.util.Arrays;
import java.util.Comparator;
class SortArray5
{
static public void main(String...alt)
{
Integer[] a={12,3,5,48,1,6,47,9,2}; //Autoboxing[Integer.valueOf(int a)]
out.println("Before Sorting : ");
for(int x:a) //Auto Unboxing[Integer.intValue()]
out.print(x+" ");
Arrays.sort(a,new MyComparator());
out.println("\n\nAfter Sorting : ");
for(int x:a) //Auto Unboxing[Integer.intValue()]
out.print(x+" ");
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Integer i1=(Integer)o1;
Integer i2=(Integer)o2;
if(i1>i2)
return -1;
else
return 1;
}
}
import java.util.Arrays;
import java.util.Comparator;
class SortArray5
{
static public void main(String...alt)
{
Integer[] a={12,3,5,48,1,6,47,9,2}; //Autoboxing[Integer.valueOf(int a)]
out.println("Before Sorting : ");
for(int x:a) //Auto Unboxing[Integer.intValue()]
out.print(x+" ");
Arrays.sort(a,new MyComparator());
out.println("\n\nAfter Sorting : ");
for(int x:a) //Auto Unboxing[Integer.intValue()]
out.print(x+" ");
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Integer i1=(Integer)o1;
Integer i2=(Integer)o2;
if(i1>i2)
return -1;
else
return 1;
}
}
0 Comment to "Java Program to sort Object/Integer Array Elements in descending order with customized sorting order."
Post a Comment