if (sortByRows) {
int begCol = ((Book)sheet.getWorkbook()).getSpreadsheetVersion().getLastColumnIndex();
int endCol = 0;
//locate begCol/endCol of the sheet
for (int rowNum = begRow; rowNum <= endRow; ++rowNum) {
final Row row = sheet.getRow(rowNum);
if (row != null) {
begCol = Math.min(begCol, row.getFirstCellNum());
endCol = Math.max(begCol, row.getLastCellNum() - 1);
}
}
begCol = Math.max(lCol, begCol);
endCol = Math.min(rCol, endCol);
for (int colnum = begCol; colnum <= endCol; ++colnum) {
final Object[] values = new Object[keyCount];
for(int j = 0; j < keyCount; ++j) {
final Row row = sheet.getRow(keyIndexes[j]);
final Cell cell = row != null ? row.getCell(colnum, Row.RETURN_BLANK_AS_NULL) : null;
final Object val = getCellObject(cell, dataOptions[j]);
values[j] = val;
}
final SortKey sortKey = new SortKey(colnum, values);
sortKeys.add(sortKey);
}
if (!sortKeys.isEmpty()) {
final Comparator<SortKey> keyComparator = new KeyComparator(descs, matchCase, sortMethod, type);
Collections.sort(sortKeys, keyComparator);
return BookHelper.assignColumns(sheet, sortKeys, begRow, lCol, endRow, rCol);
} else {
return null;
}
} else { //sortByColumn, default case
for (int rownum = begRow; rownum <= endRow; ++rownum) {
final Row row = sheet.getRow(rownum);
if (row == null) {
continue; //nothing to sort
}
final Object[] values = new Object[keyCount];
for(int j = 0; j < keyCount; ++j) {
final Cell cell = row.getCell(keyIndexes[j], Row.RETURN_BLANK_AS_NULL);
final Object val = getCellObject(cell, dataOptions[j]);
values[j] = val;
}
final SortKey sortKey = new SortKey(rownum, values);
sortKeys.add(sortKey);