Skip to content

Instantly share code, notes, and snippets.

@edm00se
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edm00se/34bc3a534c7e44ff5617 to your computer and use it in GitHub Desktop.
Save edm00se/34bc3a534c7e44ff5617 to your computer and use it in GitHub Desktop.
Sample use of custom comparator in sorting a List<javax.faces.model.SelectItem> by the label, ascending. This is implemented inside a managed bean, fully registered in faces-config, with binding via EL in a sample XPage.
<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
<managed-bean>
<managed-bean-name>myBean</managed-bean-name>
<managed-bean-class>com.eric.test.SampleComparatorUse</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<!--AUTOGEN-START-BUILDER: Automatically generated by IBM Domino Designer. Do not modify.-->
<!--AUTOGEN-END-BUILDER: End of automatically generated section-->
</faces-config>
package com.eric.test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import javax.faces.model.SelectItem;
public class SampleComparatorUse implements Serializable {
private static final long serialVersionUID = 1L;
private String selectedOption;
private List<SelectItem> selectOptionsList;
public SampleComparatorUse() {}
/**
* Custom Comparator, for use with sorting (ascending) a List<SelectItem>.
*/
private static class LabelAscComparator implements Comparator<SelectItem> {
//uses a one-off cmoparison which returns comparison boolean, as int
public int compare(SelectItem s1, SelectItem s2) {
//you can also do a case sensitive via s1.getLabel().compareTo(s2.getLabel())
return s1.getLabel().compareToIgnoreCase(s2.getLabel());
}
}
/**
* Getter for Combo Box options, sorted alphabetically ascending
* by the label. Read-only, as it's a computed value, so no setter.
*/
public List<SelectItem> getSelectOptionsList() {
if( this.selectOptionsList == null ) {
List<SelectItem> options = new ArrayList<SelectItem>();
//normally I compute this by pulling in values from another source, iterated
//these are statically added for demonstrative purposes
options.add(new SelectItem( "value3", "label3" ));
options.add(new SelectItem( "value1", "label1" ));
options.add(new SelectItem( "value2", "label2" ));
//auto-magic sorting! otherwise the order would be label3, label1, label2
//results, based on the label, in label1, label2, label3
Collections.sort( options, new LabelAscComparator() );
selectOptionsList = options;
}
return selectOptionsList;
}
/**
* @param selectedOption String value being set via the EL binding;
* this is the data field for what has been selected, standard setter.
*/
public void setSelectedOption( String selectedOption ) {
this.selectedOption = selectedOption;
}
/**
* Standard getter for the selectedOption property of the bean.
*/
public String getSelectedOption() {
return this.selectedOption;
}
}
<?xml version="1.0" encoding="UTF-8">
<xp:view>
xmlns:xp="http://www.ibm.com/xsp/core">
<xp:comboBox
id="comboBox1"
value="#{myBean.selectedOption}">
<xp:selectItems>
<xp:this.value><![CDATA[#{myBean.selectOptionsList}]]></xp:this.value>
</xp:selectItems>
</xp:comboBox>
</xp:view>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment