Home > database >  sort a 2d array using a user defined method
sort a 2d array using a user defined method

Time:04-18

I'm trying to sort a 2d array without using predefined methods or array lists.

         String[][] data = new String[][] {
                new String[] {"bob","one"},
                new String[] {"jack","two",},
                new String[] {"adam","three"}
        };

I want this to be ordered as

{"adam","three"},
{"bob","one"},
{"jack","two"},

I tried many ways I was successful to on ordering the first element but adding the second element didn't work. I'm not posting my work as it was unsuccessful Any suggestions would be highly appreciated

CodePudding user response:

Try using a Comparator along with Arrays.sort:

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        String[][] data = new String[][] {
            new String[] {"bob", "one"},
            new String[] {"jack", "two"},
            new String[] {"adam", "three"}
        };
        System.out.printf("Before: %s%n", Arrays.deepToString(data));
        Arrays.sort(data, Comparator.comparing(a -> a[0]));
        System.out.printf("After: %s%n", Arrays.deepToString(data));
    }
}

If that is not allowed here is an implementation of bubble sort as an example of not using predefined methods (you probably want to replace it with a more efficient sorting algorithm):

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[][] data = new String[][] {
            new String[] {"bob", "one"},
            new String[] {"jack", "two"},
            new String[] {"adam", "three"}
        };
        System.out.printf("Before: %s%n", Arrays.deepToString(data));
        bubbleSortByFirstElement(data);
        System.out.printf("After: %s%n", Arrays.deepToString(data));
    }

    public static void bubbleSortByFirstElement(String[][] array) {
        boolean changed;
        do {
            changed = false;
            for (int a = 0; a < array.length - 1; a  ) {
                if (array[a][0].compareTo(array[a   1][0]) > 0) {
                    String[] tmp = array[a];
                    array[a] = array[a   1];
                    array[a   1] = tmp;
                    changed = true;
                }
            }
        } while (changed);
    }
}

Output for both examples:

Before: [[bob, one], [jack, two], [adam, three]]
After: [[adam, three], [bob, one], [jack, two]]

CodePudding user response:

You can use a comparator to compare strings of the names if you are in fact sorting alphabetically by referring to the ASCII table value of letters between a—z. Notice the lowercase letters and their values. Letters 'a' has a value of 97 and 'z' a value of 122. If you use the comparator and compare the string's of the names, then you just need to make sure to put every object pair (name—number) after the first one before or after the first object in the list (bob, one in this case). This would look at jack, "is the letter j after b?, yes. Put it after bob". That's the idea, here is a string comparator guide and a get character from string (you need only the first character).

  • Related