Track or save previous index after sorting an array?

My research:

https://www.baeldung.com/java-indices-array-after-sorting

I’m using Java and not using any advanced concepts so far. What is the easiest way to do this?

My first question is - why do you want to track the index position of the unsorted values?

They’re names of employees so.

import java.util.*;

public class Example {
    public static void main(String[] args) {
        int[][] hours = {
                {2, 4, 3, 4, 5, 8, 8},
                {7, 3, 4, 3, 3, 4, 4},
                {3, 3, 4, 3, 3, 2, 2},
                {9, 3, 4, 7, 3, 4, 1},
                {3, 5, 4, 3, 6, 3, 8},
                {3, 4, 4, 6, 3, 4, 4},
                {3, 7, 4, 8, 3, 8, 4},
                {6, 3, 5, 9, 2, 7, 9}
        };
        int[] weeklyHrs = new int[hours.length];
        for (int i = 0; i < hours.length; i++) {
            for (int j = 0; j < hours[i].length; j++) {
                weeklyHrs[i] = weeklyHrs[i] + hours[i][j];
            }
        }
        for (int i = 0; i < weeklyHrs.length; i++) {
            System.out.print("Employee" + i + ":" + weeklyHrs[i]);
            System.out.println();
        }
        System.out.println();
        int[][] wHTwoD = new int[8][2];
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 2; j++) {
                if (j % 2 == 0) {
                    wHTwoD[i][j] = i;
                } else {
                    wHTwoD[i][j] = weeklyHrs[i];
                }
            }
        }

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print(wHTwoD[i][j]);
            }
            System.out.println();
        }

        for (int i = 0; i < weeklyHrs.length; i++) {
            System.out.print(weeklyHrs[i] + " ");
        }
    }

    public static void sort(int[] arr) {
        int temp;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i; j < arr.length; j++) {
                if (arr[j] < arr[i]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
}



Here’s the code that might help you understand what am I upto.

I’ve not been taught how to sort a 2d array based on the second column value, so I won’t do that.

Now, my concern is basically the title.

1 Like

Sorry, still not following. Do you have a link to the problem you are solving?

Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employee’s seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in increasing order of the total hours.

Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employee's seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in increasing order of the total hours.

        int[][] hours = {
                {2, 4, 3, 4, 5, 8, 8},
                {7, 3, 4, 3, 3, 4, 4},
                {3, 3, 4, 3, 3, 2, 2},
                {9, 3, 4, 7, 3, 4, 1},
                {3, 5, 4, 3, 6, 3, 8},
                {3, 4, 4, 6, 3, 4, 4},
                {3, 7, 4, 8, 3, 8, 4},
                {6, 3, 5, 9, 2, 7, 9}
        };

Hours array looks like this.

I want to find:

  • total weekly hours of each employee.

  • and sort them in increasing order of work hours.

I can easily find the total weekly hours of each employee.

Like this:

        for (int i = 0; i < hours.length; i++) {
            for (int j = 0; j < hours[i].length; j++) {
                weeklyHrs[i] = weeklyHrs[i] + hours[i][j];
            }
        }

Now, my concern is how do I track earlier indices. Since those indices are actually the employee id. I can’t change them like that.

So I thought why not use a 2d array instead of 1d array for weeklyHrs.

        int[][] wHTwoD = new int[8][2];
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 2; j++) {
                if (j % 2 == 0) {
                    wHTwoD[i][j] = i;
                } else {
                    wHTwoD[i][j] = weeklyHrs[i];
                }
            }
        }

I used a 2d array like this. But the concern now is to sort it based on column 1’s values i.e the hours.

Can anyone guide me how can I solve this problem?

import java.util.*;

public class Example {
    public static void main(String[] args) {
        int[][] wh2d = {
                {0, 34},
                {1, 28},
                {2, 20},
                {3, 31},
                {4, 32},
                {5, 28},
                {6, 37},
                {7, 41}
        };
        boolean isSwapped = false;
        int k = 0;
        for (int i = 0; i < wh2d.length; i++) {
            for (int j = wh2d[i].length - 1; j >= 0; j--) {
                // do sort if j%2==0
                // sort wh2d[i][j]
                if (j == 1) {
                    // sort it
                    for (k = i + 1; k < wh2d.length; k++) {
                        if (wh2d[i][j] < wh2d[k][j]) {
                            // swap
                            int temp = wh2d[i][j];
                            wh2d[i][j] = wh2d[k][j];
                            wh2d[k][j] = temp;
                            isSwapped = true;


                            // swap the corresponding indices
                            int temp2 = wh2d[i][0];
                            wh2d[i][0] = wh2d[k][0];
                            wh2d[k][0] = temp2;

                        }
                    }


                }

            }
        }

        for (int i = 0; i < wh2d.length; i++) {
            for (int j = 0; j < wh2d[i].length; j++) {
                System.out.print(wh2d[i][j] + " ");
            }
            System.out.println();
        }
    }

}

I’ve solved it lol. too hardcoded though.

That is one way to do it! The way I might do this would be to create a new array that stores two values:

[“emp0”, totalHours]
[“emp1”, totalHours]
[“emp2”, totalHours]

This will make the sorting much MUCH easier.