简体   繁体   中英

java - Sorting a 2d array by the total of one column issue

I am trying to create a method that will take a randomly filled 2D array (each row is a worker, and each column is a year they worked over the summer. Each element in the array is how much money they made that summer) and sort by the worker who made the most total money from every summer worked and sort from greatest to least from there. Here is the class:

import java.util.Scanner;

public class SummerStats
{

int[][] salaries;

SummerStats(int people, int years)
{
  int i;
  int j;
  salaries = new int[years][people];

  for (i = 0; i < salaries.length; i++) // initialize salaries[][] with random salaries
  {
     for (j = 0; j < salaries[i].length; j++)
     {
        salaries[i][j] = (int)(Math.random() * 1001);
     }
  }

}

// I have omitted a few methods here that are not relevant to this question

     // method returns sorted array by earnings of a person over all years
   public int[][] sortedEarnings()
{
  int i = 0;
  int j = 0;
  int[][] sorted = new int[salaries.length][salaries[i].length];
  int rowSum = 0;
  int row[][] = new int[2][salaries.length];
  boolean flag = true;
  int temp = 0;


  // sorted will be a copy of salaries, then be sorted by total of each row
  for (i = 0; i < salaries.length; i++)
  {
     for (j = 0; j < salaries[i].length; j++)
     {
        sorted[i][j] = salaries[i][j];
     }
  }

  // sum rows   
  for (i = 0; i < sorted.length; i++)
  {
     for (j = 0; j < sorted[i].length; j++)
     {
        rowSum = rowSum + sorted[i][j];
     }
     row[1][i] = rowSum;
     rowSum = 0;
  }

  // set worker index
  i = 0;
  for (j = 0; j < row[i].length; j++)
  {
     row[i][j] = (j + 1);
  }

  // sort by total earned for each worker
  i = 1; 
  while (flag)
  {
     flag = false;
     for (j = 0; j < row[i].length - 1; j++)
     {    
        if (row[i][j] < row[i][j + 1]) // if total amount is less than next position then swap
        {
           temp = row[i - 1][j]; // only need to sort the indeces, not the actual sums
           row[i - 1][j] = row[i - 1][j + 1];
           row[i - 1][j + 1] = temp;
           flag = true;
        }
     }
  }


  return row; 
 }

}

The idea now is, You create an identical array to the randomly generated one, then create rowSum which only finds the sum of each workers total salaries, create an index of worker numbers, then sort by the sum of salaries and swap out the worker numbers accordingly. For some reason my bubble sort just is not working, I have tried it using two for loops, as well as this while loop idea (which currently results in an unending loop, so be careful).

If anyone could find whats wrong with this sort or offer me a better method of sorting that would be great!

Your salaries 2D array is inverted. You said:

each row is a worker, and each column is a year

but you allocate as new int[years][people] which means that years are the rows and people are the columns, and if you try to sort the outer array, you're sorting across years.

Change to new int[people][years] . Then you can sort the outer array using Arrays.sort(T[] a, Comparator<? super T> c) .

Arrays.sort(salaries, new Comparator<int[]>() {
    @Override
    public int compare(int[] worker1, int[] worker2) {
        int total1 = sum(worker1);
        int total2 = sum(worker2);
        return Integer.compare(total1, total2);
    }
});

Using the following helper method:

static int sum(int[] worker) {
    int sum = 0;
    for (int salary : worker)
        sum += salary;
    return sum;
}

It's not going to be especially fast, since it has to re-sum on every compare. To improve that, you should create a Worker class, instead of relying on 2D arrays.

You can then make the Worker class cache/pre-calculate the totalSalary , and make it implement Comparable , so you won't need a Comparator .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM