Filled Under: Array
How To Sort Numbers In Descending Order Java
There are many situation in which we need to sort numbers or word this tutorial provides a simple way to do this for string see this how to sort string in java .There are two type of number sorting ascending sorting and descending sorting in this tutorial we show how to sort number in descending order for ascending order sorting see ascending sorting in java.
CODING:
class intsort
{
public static void main(String args[])
{
int arr[] = {10,20,40,60,70,100};
System.out.println("Before sorting:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
System.out.println("After sorting:");
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[j]>arr[i])
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
System.out.println(arr[i]);
}
}
}
OUTPUT:
0 comments:
Post a Comment