# Listboxes fed by list models apparently do not behave as expected if automatic sorting is used

cvarona
554 1 6

The development guide says

List boxes support sorting of list items directly. There are a few ways to enable the sorting of a particular column. The simplest way is to set the sort property of the list header to auto as follows. Then, the column that the list header is associated with is sortable based on the label of each list cell of the specified column.

This is absolutely true of directly populated listboxes, but it fails to describe the reality when it comes to describe the behaviour of 'live' lists.

<zk>
<zscript>
List data = new ArrayList();
for(int j=0; j < 20; ++j) {
data.add( new Integer( j ) );
}
ListModel strset = new SimpleListModel(data);

renderer = new ListitemRenderer() {
public void render( Listitem li, Object value ) {
li.setValue( value );
new Listcell( "" + value ).setParent( li );
new Listcell( "" + Math.random() ).setParent( li );
}
};
</zscript>
<listbox id="list" width="300px" rows="20" model="${strset}" itemRenderer='${renderer}'>
<listheader width='150px' label="Same as model" sort="auto"/>
</listbox>
</zk>

When trying up this zul it becomes clear that it's only the first column that gets ordered correctly. This is so because the default listitem comparator reads like

public int compare(Object o1, Object o2) {
final int index =

Object v1, v2;
if (o1 instanceof Listitem) {...

and it's not listitems that the comparator is provided with, but the list model inner _list instead.

Whether this is done this way by mistake or on purpose it would be nice if the default automatic sorting behaviour was available on live lists as well. Is there any way to "force" my listmodel-fed list to behave like a simple one when it comes to sorting?

With kind regards

César Varona

delete retag edit

## 7 Replies

PeterKuo
481 2

But I trace the code, in ListitemComparator.java,

public int compare(Object o1, Object o2) {
...
if (o1 instanceof Listitem) { //not live data
...
}
else { //live data
...
}
}

Maybe it's already handled by fresh code?

PeterKuo
481 2

Look at your code the second time,
I found your code generate random label each time when render.
And that's why you can't see the sorted result.

cvarona
554 1 6

Of course it does; I don't think it invalidates the test, for I guess the items get just ordered, not re-rendered, but just try

<zk>
<zscript><![CDATA[
Map mappings = new HashMap();
List data = new ArrayList();
for(int j=0; j < 20; ++j) {
data.add( new Integer( j ) );
mappings.put( data.get( j ), Math.random() );
}
ListModel strset = new SimpleListModel(data);

renderer = new ListitemRenderer() {
public void render( Listitem li, Object value ) {
li.setValue( value );
new Listcell( "" + value ).setParent( li );
new Listcell( "" + mappings.get( value ) ).setParent( li );
}
};
]]></zscript>
<listbox id="list" width="300px" rows="20" model="${strset}" itemRenderer='${renderer}'>
<listheader width='150px' label="Same as model" sort="auto"/>
</listbox>
</zk>
which ensures that every value is associated to the same random number every time.

The point is: when resorting to automatic sorting live lists get sorted on a listitem value basis, no matter which label appears in the listcells of the "sort by" column. In order to sort by listcell label you cannot use 'auto', you have to provide some comparator with the ability to tell which label matches a given listitem value for a certain column and use it to perform the comparison. However I think it would be very useful to have the ability of letting "automatic" (i.e., on a listecell basis) sorting also work on live lists.

With kind regards

César Varona

PeterKuo
481 2

Yes, I think you are right.
Please post it to feature request.

Thank you.

cvarona
554 1 6

Posted!

With kind regards

César Varona

henrichen
3869 2

See this smalltalk

http://docs.zkoss.org/wiki/Multiple_Field_Sorting_on_Listbox

cvarona
554 1 6

The zk3.5.3 sorting capabilities look nice; unfortunately enough our data model is not comprised of beans, but I guess it will do for most people.

With kind regards

César Varona

[hide preview]