如何概括实施Java 8中的Excel的实施?
我有以下服务类别:
public class SheetSorter {
// sorts the sheet
public static Sheet sortSheet(Sheet sheet) {
List<String[]> list = sheet.getSheet().stream().collect(Collectors.toList());
Collections.sort(list, new Comparator<String[]>() {
@Override
public int compare(String[] arr1, String[] arr2) {
if (Long.parseLong(arr1[3]) == Long.parseLong(arr2[3])) {
return (int) (Long.parseLong(arr1[4]) - Long.parseLong(arr2[4]));
}
else {
return (int) (Long.parseLong(arr1[3]) - Long.parseLong(arr2[3]));
}
}
});
sheet.setSheet(list);
return sheet;
}
}
如您所见,sortsheet
方法在上实现了
在sort
on 第4列
和然后
第5列
上。有什么方法可以将其转换为Integer ... columnorder
作为参数它“ thenbys'?)在该顺序提供的其余论点上?
编辑:
@Holger的建议
// sorts the sheet
public static Sheet sortSheet(Sheet sheet) {
List<String[]> list = new ArrayList<>(sheet.getSheet());
Collections.sort(list, new Comparator<String[]>() {
@Override
public int compare(String[] arr1, String[] arr2) {
if (Long.parseLong(arr1[3]) == Long.parseLong(arr2[3])) {
return Long.compare(Long.parseLong(arr1[4]), Long.parseLong(arr2[4]));
}
else {
return Long.compare(Long.parseLong(arr1[3]), Long.parseLong(arr2[3]));
}
}
});
sheet.setSheet(list);
return sheet;
}
I have the following service class:
public class SheetSorter {
// sorts the sheet
public static Sheet sortSheet(Sheet sheet) {
List<String[]> list = sheet.getSheet().stream().collect(Collectors.toList());
Collections.sort(list, new Comparator<String[]>() {
@Override
public int compare(String[] arr1, String[] arr2) {
if (Long.parseLong(arr1[3]) == Long.parseLong(arr2[3])) {
return (int) (Long.parseLong(arr1[4]) - Long.parseLong(arr2[4]));
}
else {
return (int) (Long.parseLong(arr1[3]) - Long.parseLong(arr2[3]));
}
}
});
sheet.setSheet(list);
return sheet;
}
}
As you can see, the sortSheet
methods implements sort
on column 4
and thenby
on column 5
. Is there any way I can convert this to take an Integer... columnOrder
as parameter so that this will run sort on columnOrder[0]
and 'thenBies' (or is it 'thenBys'?) on the remaining arguments supplied in that order?
EDIT:
@Holger's Suggestion
// sorts the sheet
public static Sheet sortSheet(Sheet sheet) {
List<String[]> list = new ArrayList<>(sheet.getSheet());
Collections.sort(list, new Comparator<String[]>() {
@Override
public int compare(String[] arr1, String[] arr2) {
if (Long.parseLong(arr1[3]) == Long.parseLong(arr2[3])) {
return Long.compare(Long.parseLong(arr1[4]), Long.parseLong(arr2[4]));
}
else {
return Long.compare(Long.parseLong(arr1[3]), Long.parseLong(arr2[3]));
}
}
});
sheet.setSheet(list);
return sheet;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种简单的方法是,仅根据int数组组成
比较器
:comp
是通过基于<< compare> comparator s基于<< s创建的代码>索引[0] 。An easy way is to just compose your
Comparator
based on the int array:comp
above is created by composing otherComparator
s based on indices afterindices[0]
.我的解决方案基于公认的答案:
My Solution based on the accepted answer: