Processing...
Description & Source Code

This demo shows how to implement a java.lang.comparator class to control the sorting behavior. In this demo, the sorting rule could be changed dynamically at the Configuration Panel.

sort_by_comparator.zul
<zk>
	<zscript><![CDATA[
		import java.util.Comparator;
		import demo.listbox.advanced_sorting.DemoComparator;
		ListModel cons = new ListModelList(new demo.data.ContributorData().getContributors());
		Comparator cTitleAsc = new DemoComparator(true, 1);
		Comparator cTitleDsc = new DemoComparator(false, 1);
		Comparator cFristNameAsc = new DemoComparator(true, 2);
		Comparator cFristNameDsc = new DemoComparator(false, 2);
		Comparator cLastNameAsc = new DemoComparator(true, 3);
		Comparator cLastNameDsc = new DemoComparator(false, 3);
		Comparator cExtAsc = new DemoComparator(true, 4);
		Comparator cExtDsc = new DemoComparator(false, 4);
	]]></zscript>
	<listbox model="${cons}">
		<auxhead>
			<auxheader colspan="3" class="topic">Contributor List</auxheader>
		</auxhead>
		<listhead>
			<listheader label="Title" width="150px" align="center" sortAscending="${cTitleAsc}" sortDescending="${cTitleDsc}" />
			<listheader id="nameCol" label="Full Name" align="center" sortAscending="${cFristNameAsc}" sortDescending="${cFristNameDsc}" />
			<listheader label="Extension" width="150px" align="center" sortAscending="${cExtAsc}" sortDescending="${cExtDsc}" />
		</listhead>
		
		<template name="model">
			<listitem>
				<listcell label="${each.title}" />
				<listcell label="${each.fullName}" />
				<listcell label="${each.extension}" />
			</listitem>
		</template>
	</listbox>
</zk>
sort_by_comparator_ctrl.zul
<zk>
	<groupbox sclass="z-demo-config" closable="false">
		<caption label="Sorting Rule" />
		<radiogroup>
			<attribute name="onCheck"><![CDATA[
	String sel = self.getSelectedItem().getValue();
	if (sel.equals("First")) {
		nameCol.setSortAscending(cFristNameAsc);
		nameCol.setSortDescending(cFristNameDsc);
	} else {
		nameCol.setSortAscending(cLastNameAsc);
		nameCol.setSortDescending(cLastNameDsc);
	}
]]></attribute>
			<vlayout>
				<radio label="By First Name" value="First" checked="true" />
				<radio label="By Last Name" value="Last" />				
			</vlayout>
		</radiogroup>
	</groupbox>
</zk>
Contributor.java
package demo.data.pojo;

import org.zkoss.bind.annotation.DependsOn;

public class Contributor {
	private String title;
	private String firstName;
	private String lastName;
	private Integer extension;

	public Contributor(String firstName, String lastName, String title, Integer ext) {
		setFirstName(firstName);
		setLastName(lastName);
		setTitle(title);
		setExtension(ext);
	}

	public Contributor() {
		setFirstName("");
		setLastName("");
		setTitle("Code");
		setExtension(12345);
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String fn) {
		firstName = fn;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String ln) {
		lastName = ln;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String tl) {
		title = tl;
	}

	public Integer getExtension() {
		return extension;
	}

	public void setExtension(Integer ext) {
		extension = ext;
	}

	@DependsOn({ "firstName", "lastName" })
	public String getFullName() {
		return getFirstName() + " " + getLastName();
	}
}
ContributorData.java
package demo.data;

import java.util.ArrayList;
import java.util.List;

import demo.data.pojo.Contributor;
import demo.data.pojo.LanguageContribution;

public class ContributorData {

	private List<String> titles = new ArrayList<String>();
	private List<Contributor> contributors = new ArrayList<Contributor>();
	private List<LanguageContribution> languageContributions = new ArrayList<LanguageContribution>();

	public ContributorData() {
		titles.add("Code");
		titles.add("Bug");
		titles.add("Docs");
		titles.add("Arts");

		contributors.add(new Contributor("Kaleb", "Leonel", titles.get(0).toString(), 321));
		contributors.add(new Contributor("Balu", "Haben", titles.get(0).toString(), 321));
		contributors.add(new Contributor("Trey", "Wyatt", titles.get(0).toString(), 323));
		contributors.add(new Contributor("Balu", "Chen", titles.get(0).toString(), 324));
		contributors.add(new Contributor("Terry", "Tornado", titles.get(0).toString(), 711));
		contributors.add(new Contributor("Jesse", "Miles", titles.get(1).toString(), 712));
		contributors.add(new Contributor("Sadira", "Jobs", titles.get(1).toString(), 713));
		contributors.add(new Contributor("Jaquan", "Frederick", titles.get(2).toString(), 451));
		contributors.add(new Contributor("Avery", "Katrina", titles.get(2).toString(), 453));
		contributors.add(new Contributor("Heidi", "Nikolas", titles.get(2).toString(), 455));
		contributors.add(new Contributor("Katelyn", "Clara", titles.get(2).toString(), 457));
		contributors.add(new Contributor("Branden", "Shane", titles.get(2).toString(), 459));
		contributors.add(new Contributor("Dacey", "Obert", titles.get(2).toString(), 450));
		contributors.add(new Contributor("Julianna", "Allison", titles.get(3).toString(), 643));
		contributors.add(new Contributor("Rachel", "Elisabeth", titles.get(3).toString(), 644));
		contributors.add(new Contributor("Clarissa", "Francesca", titles.get(3).toString(), 645));
		contributors.add(new Contributor("Gabby", "Taffy", titles.get(3).toString(), 646));
		
		languageContributions.add(new LanguageContribution("Arabic (ar)", "Ayman Elgharabawy", "iso-8859-6"));
		languageContributions.add(new LanguageContribution("Hungarian (hu)", "Andreas Klein", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Italian (it)", "Matteo Barbieri", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Indonesian (id)", "James Liam Supangkat", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Japanese (ja)", "Poli Lee", " iso-2022-jp"));
		languageContributions.add(new LanguageContribution("Korean (ko)", "Deok-su Lee", "euc-kr"));
		languageContributions.add(new LanguageContribution("Dutch (nl)", "Wido Jansen", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Portuguese (pt)", "Airton Carrara", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Brazilian Portugese (pt_BR)", "Airton Carrara", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Romanian (ro)", "Calin Vaida", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Russian (ru)", "Denis Yarkovoy", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("French (fr)", "Jêrôme Vergereau", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Spanish (es)", "Daniel Octavio Ruiz Rodriguez", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("German (de)", "H.-Dirk Schmitt", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Czech (cs)", "Pavel Mica", "iso-8859-2"));
		languageContributions.add(new LanguageContribution("Catalan (ca)", "Xavier Covas O'Ryan", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Bulgarian (bg) ", "Boril Yonchev", "iso-8859-5"));
		languageContributions.add(new LanguageContribution("Ukrainian (uk)", "Denis Yarkovoy", "iso-8859-5"));
		languageContributions.add(new LanguageContribution("Turkish (tr)", "Kursat Kurt", "iso-8859-9"));
		languageContributions.add(new LanguageContribution("Swedish (sv)", "Easit AB", "iso-8859-1"));
		languageContributions.add(new LanguageContribution("Slovenian (sl) ", "Ziga", "iso-8859-2"));
	}

	public List<String> getTitles() {
		return titles;
	}

	public List<Contributor> getContributors() {
		return contributors;
	}

	public List<LanguageContribution> getLanguageContributors() {
		return languageContributions;
	}

}
DemoComparator.java
package demo.listbox.advanced_sorting;

import java.io.Serializable;
import java.util.Comparator;

import demo.data.pojo.Contributor;

public class DemoComparator implements Comparator<Object>, Serializable {
	private static final long serialVersionUID = -2127053833562854322L;
	
	private boolean asc = true;
	private int type = 0;

	public DemoComparator(boolean asc, int type) {
		this.asc = asc;
		this.type = type;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@Override
	public int compare(Object o1, Object o2) {
		Contributor contributor1 = (Contributor) o1;
		Contributor contributor2 = (Contributor) o2;
		switch (type) {
		case 1: // Compare Title
			return contributor1.getTitle().compareTo(contributor2.getTitle()) * (asc ? 1 : -1);
		case 2: // Compare First Name
			return contributor1.getFirstName().compareTo(contributor2.getFirstName()) * (asc ? 1 : -1);
		case 3: // Compare Last Name
			return contributor1.getLastName().compareTo(contributor2.getLastName()) * (asc ? 1 : -1);
		case 4: // Compare Extension
			return contributor1.getExtension().compareTo(contributor2.getExtension()) * (asc ? 1 : -1);
		default: // Full Name
			return contributor1.getFullName().compareTo(contributor2.getFullName()) * (asc ? 1 : -1);
		}

	}

}