### Java Program to Sort an Integer Array in either Ascending or Descending Order using Java For Loops and a Function Parameter

When you're learning Java to prepare for an Interview, Arrays is a really important concept. Sorting of an array using for loops is basic concept which everyone should understand.

We've written an article earlier to teach you how to use inbuilt Java functions to sort the characters of a string. But, if you're asked to write an algorithm yourself that can achieve this, you're at the right place!

We'll show how to do the sorting, and for an icing on the cake, we'll also show how to control the sorting order i.e. ascending or descending! This makes our function reusable for both purposes.

So, let's get started!

In order to sort an Array, we'll have to traverse through it twice. We'll do it using for loops.

The first loop will traverse the total length of the array

`for(int i=0;i<a.length;i++)`

And, the second for loop will be incrementally traversing the array for the sorting

`for(int j=i+1;j<a.length;j++)`

See how the i and j variables are interconnected?

Next concept is swapping of the values. We'll utilize a temp variable for that.

```temp=a[i];
a[i]=a[j];
a[j]=temp;```

Now, a really cool concept that' we'll utilize is, using a parameter value passed to the function to do the sorting in ascending or descending order.

`* @param order (1=asc, 2=desc)`
`if(order==1 ? a[i]>a[j] : a[i]<a[j])`

Did you see we wrote the whole logic in a single line! That's an if-else-then right there, in a single line!

Check out the complete code now:

```/**
* Sort an Integer Array in Ascending or Descending order
* @author computengine.com
* @param a
* @param order (1=asc, 2=desc)
*/
public void sortIntArray(int[] a,int order) {

System.out.println("Input array: "+Arrays.toString(a));
int temp = 0;

//loop through the whole array
for(int i=0;i<a.length;i++) {
//loop through the array linked to the earlier loop
for(int j=i+1;j<a.length;j++) {
//Assign the sort logic as per parameter passed
if(order==1 ? a[i]>a[j] : a[i]<a[j]) {
//swap the values
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Final array: "+Arrays.toString(a));
}
```

Let's check out the Output.

```CodingPractice cp = new CodingPractice();

int[] a = {2,4,5,6,3,2,4,5,566,3,22,45,1,34,3,2};

cp.sortIntArray(a, 1);```

Input array: [2, 4, 5, 6, 3, 2, 4, 5, 566, 3, 22, 45, 1, 34, 3, 2]

Final array: [1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 22, 34, 45, 566]

```CodingPractice cp = new CodingPractice();

int[] a = {2,4,5,6,3,2,4,5,566,3,22,45,1,34,3,2};

cp.sortIntArray(a, 2);```

Input array: [2, 4, 5, 6, 3, 2, 4, 5, 566, 3, 22, 45, 1, 34, 3, 2]

Final array: [566, 45, 34, 22, 6, 5, 5, 4, 4, 3, 3, 3, 2, 2, 2, 1]

That's it folks! We've seen how to sort either in ascending or descending order, controlled by a function parameter. Same concept can be applied to character arrays as well!

Here's a sample (it works too!)

```/**
* Sort a Character array in ascending or descending order
* @author computengine.com
* @param a
* @param order
*/
public void sortCharArray(char[] a,int order) {

System.out.println("Initial Array: "+Arrays.toString(a));

char temp;

for(int i=0;i<a.length;i++) {
for(int j=i+1;j<a.length;j++) {
if(order==1 ? a[i]>a[j] : a[i]<a[j]) {
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

System.out.println("Final Array: "+Arrays.toString(a));
}
```