UIComponent column = grid.findComponent(sortColumn);
String name = ColumnUtil.getColumnSorting(column);
SortOrder sortOrder = grid.getSortOrder();
if (sortOrder == null) {
sortOrder = new SortOrder();
grid.setSortOrder(sortOrder);
if (grid.getValueExpression("sortOrder") != null) {
grid.getValueExpression("sortOrder").setValue(FacesContext.getCurrentInstance().getELContext(), sortOrder);
}
}
Boolean suggested = e.getSuggestedOrder();
SortField[] fields = sortOrder.getFields();
if (fields == null) {
//If no sorting was applied at all, set sorting to current
fields = new SortField[] {new SortField(name, nextSortOrder(null, suggested))};
} else {
List<SortField> newFields = new LinkedList<SortField>(Arrays.asList(fields));
SortField newField = null;
for (Iterator<SortField> iterator = newFields.iterator(); iterator.hasNext() && newField == null; ) {
SortField sortField = (SortField) iterator.next();
if (name != null && name.equals(sortField.getName())) {
Boolean asc = sortField.getAscending();
newField = new SortField(name, nextSortOrder(asc, suggested));
iterator.remove();
}
}
if (newField == null) {
newField = new SortField(name, nextSortOrder(null, suggested));
}
newFields.add(newField);
fields = (SortField[]) newFields.toArray(new SortField[newFields.size()]);
}
sortOrder.setFields(fields);
}