Home  >  Article  >  Java  >  How to Sort a Two-Dimensional Array by a Specific Column in Java?

How to Sort a Two-Dimensional Array by a Specific Column in Java?

Linda Hamilton
Linda HamiltonOriginal
2024-11-03 11:14:03224browse

How to Sort a Two-Dimensional Array by a Specific Column in Java?

Sorting Two-Dimensional Arrays by Column in Java

The task of sorting a two-dimensional array based on a specific column presents a unique challenge in Java. To accomplish this, the following solution delves into the details of the problem and provides a comprehensive code implementation.

The provided input consists of a two-dimensional array where the first column represents a date in the format "yyyy.MM.dd HH:mm" and the second column contains strings. The objective is to sort this array by the first column, resulting in the dates being arranged in chronological order.

Implementation

The provided Java code employs the built-in Arrays.sort() method in combination with a Comparator to achieve the desired sorting. The Comparator defines a rule for comparing the array elements, specifically focusing on the first column (the date). The code compares the dates using the compareTo() method, which returns an integer value indicating the relative chronological order of the dates.

<code class="java">import java.util.Arrays;
import java.util.Comparator;

public class TwoDArraySort {

    public static void main(final String[] args) {
        final String[][] data = new String[][] {
                new String[] { "2009.07.25 20:24", "Message A" },
                new String[] { "2009.07.25 20:17", "Message G" },
                new String[] { "2009.07.25 20:25", "Message B" },
                new String[] { "2009.07.25 20:30", "Message D" },
                new String[] { "2009.07.25 20:01", "Message F" },
                new String[] { "2009.07.25 21:08", "Message E" },
                new String[] { "2009.07.25 19:54", "Message R" } };

        Arrays.sort(data, new Comparator<String[]>() {
            @Override
            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]);
        }
    }

}</code>

Output

The output of the provided code is as follows:

2009.07.25 19:54 Message R
2009.07.25 20:01 Message F
2009.07.25 20:17 Message G
2009.07.25 20:24 Message A
2009.07.25 20:25 Message B
2009.07.25 20:30 Message D
2009.07.25 21:08 Message E

This output demonstrates that the two-dimensional array has been successfully sorted by the first column, resulting in the chronological order of the dates.

The above is the detailed content of How to Sort a Two-Dimensional Array by a Specific Column in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn