简体   繁体   English

Collection.sort()基于第一顺序的第二次排序

[英]Collection.sort() second sorting based on first ordering

I have a list of questions in a List<RSSQuestion> , which I am trying to sort: first with their keyActivity , then in each keyActivity there are subActivities ; 我正在尝试排序的List<RSSQuestion>有一个问题List<RSSQuestion> :首先是它们的keyActivity ,然后在每个keyActivity中都有subActivities I need to sort out the questions based on the subActivities . 我需要根据subActivities整理问题。

For example: 例如:

  1. OutLine Design (keyActivity) 外观设计(keyActivity)
    1. SubActivity One 子活动一
  2. Specification (keyActivity) 规格(keyActivity)
    1. Overview 总览
    2. Backlog 积压

The code that I have is as follows, which is sorting the questions based on the keyActivity , but I don't know how can I do the second sorting based on the subActivity : 我拥有的代码如下,该代码基于keyActivity排序问题,但是我不知道如何基于subActivity进行第二次排序:

private static List<RSSQuestion> sortingExtractedData(List<RSSQuestion> extractedDataByKeyactivity) {
    List<RSSQuestion> exctractedData = new ArrayList<>();

    Collections.sort(extractedDataByKeyactivity, new Comparator<RSSQuestion>() {

        @Override
        public int compare(RSSQuestion o1, RSSQuestion o2) {
            return o1.getKeyActivity().compareTo(o2.getKeyActivity());
        }
    });

    for (RSSQuestion rssQuestion : extractedDataByKeyactivity) {
        Collections.sort(rssQuestion.getSubActivity(), new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
    }

    exctractedData.addAll(extractedDataByKeyactivity);
    return exctractedData;
}

Please let me know, how can I do the sorting for the rest subActivities ? 请让我知道,如何对其余subActivities进行排序?

It should all be done with one Comparator , but you only go to the SubActivity when the KeyActivity is the same (ie keyActivityCompare == 0 ): 所有这些都应该由一个Comparator完成,但是只有在KeyActivity相同时(即keyActivityCompare == 0 ),您才转到SubActivity

Collections.sort(extractedDataByKeyactivity, new Comparator<RSSQuestion>() {

    @Override
    public int compare(RSSQuestion o1, RSSQuestion o2) {
        int keyActivityCompare = o1.getKeyActivity().compareTo(o2.getKeyActivity());
        if (keyActivityCompare == 0)
          return o1.getSubActivity().compareTo(o2.getSubActivity());
        return keyActivityCompare;
    }
});

Collections.sort is stable : Collections.sort稳定的

the algorithm used by sort does not have to be a mergesort, but it does have to be stable. sort使用的算法不必是mergesort,但必​​须稳定。

That means that if two elements have the same value, the sorting does not change their order. 这意味着,如果两个元素具有相同的值,则排序不会更改其顺序。 You can make use of this by first sorting on sub activity and then on key activity. 您可以通过首先对子活动进行排序然后对关键活动进行排序来利用此功能。

As an example, I sort tuples of numbers, first on the first element, then on the second. 例如,我对数字元组进行排序,首先在第一个元素上,然后在第二个元素上。 Input: 输入:

(4, 2), (1, 0), (3, 3), (4, 1)

First sort on second element: 首先对第二个元素进行排序:

(1, 0), (4, 1), (4, 2), (3, 3)

Then sort again on first element: 然后在第一个元素上再次排序:

(1, 0), (3, 3), (4, 1), (4, 2)

This way of sorting is reminiscent of radix sort . 这种排序方式让人想起基数排序

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

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