如何概括实施Java 8中的Excel的实施?

发布于 2025-01-24 17:30:24 字数 1765 浏览 0 评论 0原文

我有以下服务类别:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2025-01-31 17:30:24

一种简单的方法是,仅根据int数组组成比较器

public static Sheet sortSheet(Sheet sheet, int...indices) {
    
    if(indices.length == 0) throw new IllegalArgumentException();
    
    Comparator<String[]> comp = Comparator.comparingLong(s -> Long.parseLong(s[indices[0]]));
    for(int i = 1; i < indices.length; i++) {
        int e = i;
        comp = comp.thenComparing(Comparator.comparingLong(s -> Long.parseLong(s[indices[e]])));
    }
    
    List<String[]> list = sheet.getSheet().stream().collect(Collectors.toList());
    Collections.sort(list, comp);
    sheet.setSheet(list);
    
    return sheet;
}

comp是通过基于<< compare> comparator s基于<< s创建的代码>索引[0] 。

An easy way is to just compose your Comparator based on the int array:

public static Sheet sortSheet(Sheet sheet, int...indices) {
    
    if(indices.length == 0) throw new IllegalArgumentException();
    
    Comparator<String[]> comp = Comparator.comparingLong(s -> Long.parseLong(s[indices[0]]));
    for(int i = 1; i < indices.length; i++) {
        int e = i;
        comp = comp.thenComparing(Comparator.comparingLong(s -> Long.parseLong(s[indices[e]])));
    }
    
    List<String[]> list = sheet.getSheet().stream().collect(Collectors.toList());
    Collections.sort(list, comp);
    sheet.setSheet(list);
    
    return sheet;
}

comp above is created by composing other Comparators based on indices after indices[0].

不忘初心 2025-01-31 17:30:24

我的解决方案基于公认的答案:

public static Sheet sortSheet(Sheet sheet, Integer... indices) {

    List<String[]> list = new ArrayList<>(sheet.getSheet());
    Collections.sort(list, new Comparator<String[]>() {
        
        @Override
        public int compare(String[] arr1, String[] arr2) {
            for (int i = 0; i < indices.length;) {
                if (Long.parseLong(arr1[i]) == Long.parseLong(arr2[i])) {
                    i++;
                }
                else {
                    return Long.compare(Long.parseLong(arr1[i]), Long.parseLong(arr2[i]));
                }
            }
            return 0;
        }
    });
    sheet.setSheet(list);
    return sheet;
}

My Solution based on the accepted answer:

public static Sheet sortSheet(Sheet sheet, Integer... indices) {

    List<String[]> list = new ArrayList<>(sheet.getSheet());
    Collections.sort(list, new Comparator<String[]>() {
        
        @Override
        public int compare(String[] arr1, String[] arr2) {
            for (int i = 0; i < indices.length;) {
                if (Long.parseLong(arr1[i]) == Long.parseLong(arr2[i])) {
                    i++;
                }
                else {
                    return Long.compare(Long.parseLong(arr1[i]), Long.parseLong(arr2[i]));
                }
            }
            return 0;
        }
    });
    sheet.setSheet(list);
    return sheet;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文