我想在List<RSSQuestion>
中列出问题列表,尝试对它们进行排序:首先是它们的keyActivity
,然后在每个keyActivity
中都有subActivities
;我需要根据subActivities
整理问题。
例如:
外观设计(keyActivity)
子活动一
规格(keyActivity)
总览
积压
我拥有的代码如下,该代码基于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;
}
请让我知道,如何对其余的
subActivities
进行排序? 最佳答案
都应该用一个Comparator
完成,但是只有当SubActivity
相同时(即KeyActivity
),您才能进入keyActivityCompare == 0
:
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;
}
});
关于java - Collection.sort()基于第一顺序的第二次排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22763790/