JAM*_*AID 3 java sorting arraylist
我的列表中有以下数据:
List<FeatureAnalyzeDTOResult> list = new ArrayList<>();
list.add(new FeatureAnalyzeDTOResult("october", 46));
list.add(new FeatureAnalyzeDTOResult("april", 46));
list.add(new FeatureAnalyzeDTOResult("march", 46));
list.add(new FeatureAnalyzeDTOResult("november", 30));
list.add(new FeatureAnalyzeDTOResult("may", 46));
list.add(new FeatureAnalyzeDTOResult("january", 53));
list.add(new FeatureAnalyzeDTOResult("december", 30));
Run Code Online (Sandbox Code Playgroud)
我想做什么?
我正在尝试按顺序对这些数据进行排序,以便数据按月份排序,并且月份应从当前月份开始并计算前六个月。
例如:
目前是五月,数据应按以下顺序排序:
[MAY, APRIL, MARCH, FEBRUARY, JANUARY, DECEMBER]
Run Code Online (Sandbox Code Playgroud)
如果缺少任何月份,它应该简单地跳过它并转到下个月并完成计数。
到目前为止我已经尝试过什么?
我尝试使用以下代码来获取当月和前六个月:
YearMonth thisMonth = YearMonth.now();
String[] month = new String[6];
for (int i = 0; i < 6; i++) {
YearMonth lastMonth = thisMonth.minusMonths(i);
DateTimeFormatter monthYearFormatter = DateTimeFormatter.ofPattern("MMMM");
month[i] = lastMonth.format(monthYearFormatter);
month[i] = month[i].toUpperCase();
}
List<String> monthList = Arrays.asList(month);
System.out.println(monthList);
Run Code Online (Sandbox Code Playgroud)
我也尝试过写一个Comparator,但它没有按预期工作。我对编写的逻辑有点困惑Comparator。
Comparator<FeatureAnalyzeDTOResult> comp = (o1, o2)
-> monthList.indexOf(o2.getMonth().toUpperCase()) - monthList.indexOf(o1.getMonth().toUpperCase());
list.sort(comp);
Run Code Online (Sandbox Code Playgroud)
它给出的输出如下:
[Feature: december Count: 30
, Feature: january Count: 53
, Feature: march Count: 46
, Feature: april Count: 46
, Feature: may Count: 46
, Feature: october Count: 46
, Feature: november Count: 30]
Run Code Online (Sandbox Code Playgroud)
这是FeatureAnalyzeDTOResult供参考的类:
class FeatureAnalyzeDTOResult {
private String month;
private int count;
public FeatureAnalyzeDTOResult(String feature, int count) {
this.month = feature;
this.count = count;
}
public FeatureAnalyzeDTOResult() {
}
public String getMonth() {
return month;
}
public void setMonth(String feature) {
this.month = feature;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
StringBuilder string = new StringBuilder();
string.append("Feature: ").append(getMonth()).append(" Count: ").append(getCount()).append(" \n");
return string.toString();
}
Run Code Online (Sandbox Code Playgroud)
这是一种替代方法:
Month的枚举中获取月份列表java.timeCollections.rotate使用当前月份值旋转列表Collections.reverse就像是
import java.util.Collections;
import java.util.Comparator;
public static void main(String[] args) {
List<FeatureAnalyzeDTOResult> list = new ArrayList<>();
list.add(new FeatureAnalyzeDTOResult("october", 46));
list.add(new FeatureAnalyzeDTOResult("april", 46));
list.add(new FeatureAnalyzeDTOResult("march", 46));
list.add(new FeatureAnalyzeDTOResult("november", 30));
list.add(new FeatureAnalyzeDTOResult("may", 46));
list.add(new FeatureAnalyzeDTOResult("january", 53));
list.add(new FeatureAnalyzeDTOResult("december", 30));
List<Month> months = new ArrayList<>(Arrays.asList(Month.values()));
Collections.rotate(months, 12 - YearMonth.now().getMonthValue());
Collections.reverse(months);
Comparator<FeatureAnalyzeDTOResult> comp =
Comparator.comparingInt(f -> months.indexOf(Month.valueOf(f.getMonth().toUpperCase())));
list.stream().sorted(comp).limit(6).forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)