Home > OS >  How to generalize the implementation the .thenby of excel in Java 8?
How to generalize the implementation the .thenby of excel in Java 8?

Time:04-29

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;
}

CodePudding user response:

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].

CodePudding user response:

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;
}
  • Related