Sunday, February 12, 2017

Java Program to sort Object/Double Array Elements in descending order with customized sorting order in a particular range .

import static java.lang.System.out;
import java.util.Arrays;
import java.util.Comparator;
class SortArray6
{
static public void main(String...alt)
{
Double[] a={10.6,15.3,10.5,2.3,6.4,1.7,9.78,2.34};
out.println("Before Sorting : ");
for(double x:a)
out.print(x+"   ");
Arrays.sort(a,1,5,new MyComparator());
out.println("\n\nAfter Sorting : ");
for(double x:a)
out.print(x+"   ");
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Double i1=(Double)o1;
Double i2=(Double)o2;
if(i1>i2)
return -1;
else
return 1;
}
}

Share this

0 Comment to "Java Program to sort Object/Double Array Elements in descending order with customized sorting order in a particular range ."

Post a Comment