Java array sort descending and print their element number

java sort array descending without using sort
sorting array in java using for loop
how to sort an array in java
sort string in descending order java
reverse array java
arrays.sort comparator
sort array in ascending order java
sort string array in java

What I am trying to do is sort an array in descending order like this


and then print which array element was first and then second and then thrird etc..

INPUT: 10 43 77 23 the descending order of them is: 77 43 23 10 I want it to print: 3 2 4 1

If you have any questions feel free to ask me.

Thanks you in advance!

You could work with a Map with the element as the key, and the index as the value. You'd first have to create this map with a for loop

Map<Integer, Integer> indexLookUp = new HashMap<>();
for(int i = 0; i < array.length; i++) {
    map.put(array[i], i);

Then you can sort the array:


You don't need the reversing part, as you can just iterate the array backwards. And then print out the index for the element via the map lookup:

for(int i = array.length - 1; i >= 0; i--) {
    System.out.println("Element: " + array[i] + ", previous index: " + map.get(array[i]));

Different ways to sort an array in descending order in C# , How do you sort an array in ascending and descending order in Java? Java Program to Sort Array in Descending Order using Temp Variable This Java program allows the user to enter the size and Array elements. Next, it will sort the array element in descending order using For Loop.

If I understand correct, you want to print the index of the element in the original array? First you'll need to copy the array, sort the copy and then find the indices in of the sorted elements in the original array. Following code does that.

int[] x = Arrays.copyOf(array, array.length);

List listObject = Arrays.asList(array)

for(int el : x) {

This is assuming your elements in the array are unique. If the elements in your array are of a different type, you should change the type of x and el accordingly.

Java Array Sort - Arrays.sort() Example, How do you sort an array of objects in Java? Original array: Array after sorting: Elements will be sort in such a way that smallest element will appear on extreme left which in this case is 1. The largest element will appear on extreme right which in this case is 8. Algorithm. Declare and initialize an array. Loop through the array and select an element.

Try this:

int array[] = {17, 10, 8, 13, 5, 7, 8, 30};

// Init the element list
List<Element> elements = new ArrayList<Element>();
for (int i = 0; i < array.length; i++) {
    elements.add(new Element(i, array[i]));

// Sort and print
Collections.reverse(elements); // If you want reverse order
for (Element element : elements) {
    System.out.println(element.value + " " + element.index);

Arrays.sort() in Java with examples, A Java program to sort an array of integers in ascending order. filter_none. edit Our arr contains 8 elements. int [] arr = { 13 Output: Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]. We can also use sort() to sort a subarray of arr[] Sort subarray from index 1 to 4, i.e., in ascending and descending orders using Arrays.sort(). 2. Arrange Numbers In Descending Order Program logic, Get the total number of element count. Get input data and store it in array. Compare each element in array one by one. if 1st element is less than second element, store 1st element in temp variable and push 2nd element to 1st element position. Then move temp value to 2nd element position.

You can sort an array of indices instead of the array containing the actual data:

    Integer[] indices = new Integer[array.length];
    for (int i = 0; i < array.length; ++i) {
        indices[i] = i;
    Arrays.sort(indices, (a, b) -> {
        return array[a] < array[b] ? 1 : array[a] > array[b] ? -1 : 0;});

Sort an Array in Descending (Reverse) Order, The previous tutorial was all about sorting an array in ascending order. In this post Here we have two arrays, one is integer array and another one is String array. Output: Int Array Elements in reverse order: 30 16 15 9 2 String Array Elements in reverse order: PP OO FF DD AA Your email address will not be published. JAVA program to sort the elements of an array in ascending order. This JAVA program is to sort the elements of an array in ascending order.For example, if an array a consists of elements a={50,10,90,30} , then on sorting in ascending order we would get a={10,30,50,90}.

How to sort an Array in descending order in Java, Unfortunately, for a primitive array, there is no direct way to sort in descending order. In order to sort a reference type array-like String array, Integer array or Employee array, you need to (answer); How to print elements of an array in Java? java.util.Arrays class provides many options for sorting an Array. We will sort Array in Ascending, Descending and Mixed mode(1st/first to kth in Ascending order and k+1th to nth/last in Descending order, you can choose different position for 1st,kth and nth). In addition this question was asked in leading Multi National Company(MNC).

Java Program to Sort Array in Descending Order, Java Array sort in descending : How to write a Java Program to Sort Array in System.out.print(" Please Enter Number of elements in an array : "); Size = sc. In this java tutorial, we are sorting an array in ascending order using temporary variable and nested for loop. We are using Scanner class to get the input from user. Java Example: Program to Sort an Array in Ascending Order. In this program, user is asked to enter the number of elements that he wish to enter.

How to sort array, list, map and set in Java, Learn to sort array or collection in Java. we will learn to sort set, list and map of Integer[] numbers = new Integer[] { 15 , 11 , 9 , 55 , 47 , 18 , 520 , 1123 , 366 , 420 }; //Print array to confirm reverseOrder() comparator to reverse the default sorting behavior in one line. Given example sort the array from element 9 to 18. How can I arrange the elements in an array based on the number of occurrences of that value in ascending order in java. This is what I've tried: int a[]={0,0,0,1,3,3,2,1,3,5,6,0}; int b=a.length;

  • question 1: what have you tried so far? at this point, wed don't even know what the type of the elements is you are trying to sort. how on earth do you assume we can help you with such little information?
  • You will probably need to implement your own sorting algorithm rather than relying on Arrays.sort() and then as you sort each element do a System.out.print(elementIndex + " ")
  • Yes sorry, this is my first question here. So I tried save their order in another array but I could find a way to do it right.
  • There are many ways actually with the existing question, and all seems to be equivalent, if there is no more constraint, so it is hard to say what is the best option for you.
  • @Lino Edited the answer
  • note however that in this example, the indices are zero-based.
  • also note that it will be in reverse order: +1 is returned when array[a] < array[b] whereas -1 is return when array[a] > array[b].
  • array needs to be final in this case. One suggestion is you can just return[a], array[b]);
  • @PhamTrung It depends on the version of Java you're using, but at least, the variable array must not be modified.
  • Correct me if I am wrong, but if you can use lambda in your solution, which mean you can use