简体   繁体   English

排序具有另一个依赖于它的数组的数组

[英]Sorting array which has another array dependent on it

I have an array with dates called startDates .我有一个名为startDates日期数组。 I also have an array with dates called endDates .我还有一个名为endDates日期数组。 I also have an array with longs called dateDifferences .我还有一个名为dateDifferences长数组。 Now, I print somewhere in my code, the start date, along with the difference (calculated using startDate and endDate ) beside it.现在,我在代码中的某处打印开始日期,以及它旁边的差异(使用startDateendDate计算)。 Then I repeat this for all the Date objects.然后我对所有 Date 对象重复此操作。 However, I need to have my startDates printed in order.不过,我需要我的startDates才能打印。 How can I achieve this so that the difference is beside the respective startDate ?我怎样才能做到这一点,以便差异在各自的startDate旁边?

ArrayList <Date> startDates = new ArrayList<Date>();
ArrayList <Date> endDates = new ArrayList<Date>();
ArrayList <Long> dateDifferences = new ArrayList<Long>();

In these arrays I add the pairs startDate and endDate and dateDifference in their respective arraylists.在这些数组中,我在各自的数组列表中添加了 startDate 和 endDate 以及 dateDifference 对。

have u considered making ur three arrays one 2d array with 3 rows:你有没有考虑过让你的三个数组成为一个 3 行的二维数组:

int[][] multi = new int[3][10]

and sorting it like in this Answer?并像在这个答案中那样排序?

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

public class Asdf {

    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]);
        }
    }

}

credit to Bert F. Sort a two dimensional array based on one column归功于 Bert F. 基于一列对二维数组进行排序

It would help if you added more context and/or an example of what you wanted.如果您添加更多上下文和/或您想要的示例,这将有所帮助。 But, let me try doing that for you.但是,让我试着为你做这件事。 Based on my understanding, an example of what you want is:根据我的理解,你想要的一个例子是:

start date [1 jan 2016, 4 jan 2016, ...]
end date   [2 jan 2016, 7 jan 2016, ...]
dateDiff   [1, 3, ...]

And then, you want to print something like:然后,您想打印如下内容:

1 jan 2016 : 1 
4 jan 2016 : 3
...

There are several options you have here: 1. You don't need to necessarily need the dateDiff array.这里有几个选项: 1. 您不一定需要 dateDiff 数组。 Just compute the date difference between start and end dates when you print the start date.打印开始日期时,只需计算开始日期和结束日期之间的日期差异。 And then print the difference right there!然后在那里打印差异! 2. If you want to pre-compute it for whatever reason, consider using a better data structure for the job. 2. 如果您出于任何原因想要预先计算它,请考虑使用更好的数据结构来完成这项工作。 A map would work if you do not have duplicate start dates, but that might be a bad assumption.如果您没有重复的开始日期,地图会起作用,但这可能是一个错误的假设。 And if so, then an array of tuples might work well enough and is usually pretty performant for this type of work.如果是这样,那么一组元组可能工作得很好,并且对于此类工作通常非常高效。

If this doesn't work for you, tell us why or share more context.如果这对您不起作用,请告诉我们原因或分享更多背景信息。 You will often find more helpful solutions then!您通常会找到更多有用的解决方案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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