public static DataSet getOrdered(DataSet dataSet, final OrderByItem... orderByItems) {
if (orderByItems != null && orderByItems.length != 0) {
final int[] sortIndexes = new int[orderByItems.length];
for (int i = 0; i < orderByItems.length; i++) {
OrderByItem item = orderByItems[i];
int indexOf = dataSet.indexOf(item.getSelectItem());
sortIndexes[i] = indexOf;
}
final List<Row> data = readDataSetFull(dataSet);
if (data.isEmpty()) {
return new EmptyDataSet(dataSet.getSelectItems());
}
final Comparator<Object> valueComparator = ObjectComparator.getComparator();
// create a comparator for doing the actual sorting/ordering
final Comparator<Row> comparator = new Comparator<Row>() {
public int compare(Row o1, Row o2) {
for (int i = 0; i < sortIndexes.length; i++) {
int sortIndex = sortIndexes[i];
Object sortObj1 = o1.getValue(sortIndex);
Object sortObj2 = o2.getValue(sortIndex);
int compare = valueComparator.compare(sortObj1, sortObj2);
if (compare != 0) {
OrderByItem orderByItem = orderByItems[i];
boolean ascending = orderByItem.isAscending();
if (ascending) {
return compare;
} else {
return compare * -1;
}