-3

I wrote code that fills a two-dimensional array with data from a file.

 public static Collection getBrowser() {   
        return Arrays.asList(readFromFile("arr.txt"));
    }
    private static Object[][] readFromFile(String filePath) {
        try {
            InputStream inputStream = new FileInputStream(filePath);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder string = new StringBuilder();
            while (reader.ready()) {
                string.append(reader.readLine());
                string.append(System.getProperty("line.separator"));
            }
            reader.close();
            String[] lineSplit = string.toString().split(System.getProperty("line.separator"));
            Object[][] result = new Object[lineSplit.length][];
            for (int i = 0; i < lineSplit.length; i++) {
                String line = lineSplit[i];
                String[] itemSplit = line.split(",");
                result[i] = new Object[itemSplit.length];
                for (int j = 0; j < itemSplit.length; j++) {
                    if (j == 0) {
                        result[i][j] = Integer.valueOf(itemSplit[j]);
                    } else {
                        result[i][j] = itemSplit[j];
                    }
                  System.out.print(result[i][j]+" ");
                }
            //System.out.println();
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

Example file

 1000, a, a, x
    1000, x, x, y
    1200, c, c, y

Tell me please, how can I sort this array by the last value, by sweeping each line into a separate array

Arr1
1000, a, a, x
Arr2
1000, x, x, y
1200, c, c, y
Igor
  • 3
  • 1
  • What you want to achieve? How many values are possible in last column? – Betlista Feb 05 '18 at 13:43
  • You need to tackle one problem at a time. Build the program up a little at a time. Start with hard-coded examples before reading from files: e.g. Do you want to start with something like this`String[][] data = { {"1000", "a", "a", "x"}, {"1200", "c", "c", "y"} };` – DJDaveMark Feb 05 '18 at 13:46
  • @Betlista, in the last column there can be 2 different values. On them I have to filter the rows – Igor Feb 05 '18 at 13:47
  • @DJDaveMark Unfortunately, 1 value in the string must be int – Igor Feb 05 '18 at 13:49
  • When you read from a file, you're gonna have strings. If you can create a simpler example with hard-coded values, and show us a result you want (a hard-coded java variable), it'll be easier to help you. – DJDaveMark Feb 05 '18 at 14:05

1 Answers1

1

So far you are on a good direction, so you are able to read a file... What you want to do next?

To me it doesn't really looks like 2D array, but more like array of Objects. Define object to wrap your data and the use Comparator.

Once you'll implement Comparator, you can call Arrays.sort(array, comparator).

See some help here: How to use Comparator in Java to sort

You do not need to split to two arrays and sort separately, once you are more familiar with comparator, you can do that in one go...

Betlista
  • 10,327
  • 13
  • 69
  • 110
  • thanks for the help. I have written an autotest that takes parameters from a file. But when all these parameters are in the same array, it does not follow the order. So I want to make 3 data arrays. – Igor Feb 05 '18 at 13:56