KarmaKing KarmaKing - 1 year ago 101
Java Question

How to alphabetically sort two dimensional array in java

Lets say i have a two dimensional array:

String [][] arr = {

i would like to sort alphabetically according to column 0
so adam would be first the bob the jack - and it (or new array) will look like so:


Answer Source
import java.util.Arrays;
import java.util.Comparator;

public class demo_sort {

    public static void main(String[] args) {

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

        Arrays.sort(data, new Comparator<String[]>() {
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);

        for (final String[] s : data) {
            System.out.println(s[0] + " " + s[1]);




adam three
bob one
jack two

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download