ListBox Sorting

998 views
Skip to first unread message

sidkdbl07

unread,
Nov 6, 2007, 3:21:53 PM11/6/07
to Google Web Toolkit
I'm tring to sort a ListBox alphabetically with the following
function. Does anybody have any better ideas?

public void sortListBox(ListBox box) {
if(box.getItemCount() > 1) {
for(int i=0; i<box.getItemCount(); i++) {
if(box.getItemText(i).substring(0,3).compareTo(box.getItemText(i
+1).substring(0,3)) > 0) {
box.insertItem(box.getItemText(i), i+2);
box.removeItem(i);
}
}
}
}

abickford

unread,
Nov 6, 2007, 4:45:04 PM11/6/07
to Google Web Toolkit
java.util.Arrays is supported. Assuming your list is actually backed
by a string array:

String[] items = { "pear", "apple", "banana" };
Arrays.sort(items);

The items array is now sorted.

Peter Blazejewicz

unread,
Nov 6, 2007, 5:01:14 PM11/6/07
to Google Web Toolkit
hi,

something similiar,
I wonder what will be faster,

public class ImageViewer implements EntryPoint {
private Button clickMeButton;
private ListBox listBox;

private native void addToArray(JavaScriptObject array, int i, String
text)/*-{
array[i] = text;
}-*/;

public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
clickMeButton = new Button();
rootPanel.add(clickMeButton);
clickMeButton.setText("Sort items");
clickMeButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
sortItems(listBox);
}
});
listBox = new ListBox();
rootPanel.add(listBox, 5, 46);
listBox.addItem("banana");
listBox.addItem("apple");
listBox.addItem("orange");
listBox.addItem("pear");
listBox.addItem("grape");
listBox.setSize("163px", "328px");
listBox.setVisibleItemCount(5);
}

private void sortItems(ListBox list) {
int size = list.getItemCount();
JavaScriptObject array = JavaScriptObject.createArray();
for (int i = size - 1; i >= 0; --i) {
addToArray(array, i, list.getItemText(i));
}
updateList(array, list);
}

private native void updateList(JavaScriptObject array, ListBox list)/
*-{
array.sort();
for(i = 0; i<array.length; i++){
this.@com.mycompany.project.client.ImageViewer::updateList(Lcom/
google/gwt/user/client/ui/ListBox;ILjava/lang/String;)(list, i,
array[i]);
}
}-*/;

private void updateList(ListBox list, int index, String text) {
list.setItemText(index, text);
}
}

regards,
Peter

sidkdbl07

unread,
Nov 7, 2007, 1:23:05 AM11/7/07
to Google Web Toolkit
I guess the issue that I have is that the ListBox items have a value
and a text. I guess I would use a struct of some sort and then sort
the struct. Oh well, it gives me some ideas. Thanks. Keep the ideas
flowing.

Peter Blazejewicz

unread,
Nov 7, 2007, 3:43:09 PM11/7/07
to Google Web Toolkit
hi,

> I guess the issue that I have is that the ListBox items have a value
> and a text.
these is not recommended (i would use jre emulation instead) but it is
just working (and requires quick modification of previous sample):

public class ImageViewer implements EntryPoint {
private Button clickMeButton;
private ListBox listBox;

private native void addToArray(JavaScriptObject array, int i, String

text, String value)/*-{
array[i] = {text: text, value:value};
}-*/;

public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
clickMeButton = new Button();
rootPanel.add(clickMeButton);
clickMeButton.setText("Sort items");
clickMeButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
sortItems(listBox);
}
});
listBox = new ListBox();
rootPanel.add(listBox, 5, 46);

// use key/value
listBox.addItem("Potato", "Potatoes");
listBox.addItem("banana", "Bananas");
listBox.addItem("apple", "Apples");
listBox.addItem("orange", "Oranges");
listBox.addItem("pear", "Pears");
listBox.addItem("grape", "Grapes");


listBox.setSize("163px", "328px");
listBox.setVisibleItemCount(5);
}

private void sortItems(ListBox list) {
int size = list.getItemCount();
JavaScriptObject array = JavaScriptObject.createArray();
for (int i = size - 1; i >= 0; --i) {

addToArray(array, i, list.getItemText(i), list.getValue(i));
}
updateList(array, list);
StringBuffer sb = new StringBuffer();
for(int i = 0; i<listBox.getItemCount(); i++){

sb.append("key:").append("\t").append(listBox.getItemText(i)).append("\t");

sb.append("value:").append("\t").append(listBox.getValue(i)).append("\n");
}
Window.alert("sorted:\n"+sb.toString());
System.out.println(sb.toString());
}

private native void updateList(JavaScriptObject array, ListBox list)/
*-{

//var sort = function(
array.sort(function(a, b){
if(a.text.toLowerCase()>b.text.toLowerCase()){
return 1;
} else if(a.text.toLowerCase()<b.text.toLowerCase()){
return -1;
}
return 0;


});
for(i = 0; i<array.length; i++){
this.@com.mycompany.project.client.ImageViewer::updateList(Lcom/

google/gwt/user/client/ui/ListBox;ILjava/lang/String;Ljava/lang/
String;)(list, i, array[i].text, array[i].value);
}
}-*/;

private void updateList(ListBox list, int index, String text, String
value) {
list.setItemText(index, text);
list.setValue(index, value);
}
}

key: apple value: Apples
key: banana value: Bananas
key: grape value: Grapes
key: orange value: Oranges
key: pear value: Pears
key: Potato value: Potatoes


regards,
Peter

sidkdbl07

unread,
Nov 12, 2007, 1:26:59 PM11/12/07
to Google Web Toolkit
private native void updateList(JavaScriptObject array, ListBox list)
is not found in JSNI when i try to compile.

Any ideas?

sidkdbl07

unread,
Nov 12, 2007, 1:39:50 PM11/12/07
to Google Web Toolkit
I don't understand the following line...

th...@com.mycompany.project.client.ImageViewer::updateList(Lcom/
google/gwt/user/client/ui/ListBox;ILjava/lang/String;Ljava/lang/

What do I replace the 'th...' with?

mP

unread,
Nov 12, 2007, 3:07:55 PM11/12/07
to Google Web Toolkit
read the jsni pages that come with the download. It explains all that
you need to know about "native" java methods which are used to include
javascript within a comment. What you are seeing is the mechanism that
javascript code uses to refer to java variables/methods etc.

mP

unread,
Nov 12, 2007, 3:10:39 PM11/12/07
to Google Web Toolkit
You really should be sorting the list prior to actually creating the
ListBox items, using Arrays.sort( Object[], Comparator ). Adding/
removing items as one does when sorting is going to be much slower
than sorting a list of objects.


You can provide your own comparator if need be.
Arrays.sort( Object[] ) simply sorts if the array contains types that
are Comparable. Read the javadoc for String, java.util.Comparator,
java.util.Comparable.

Peter Blazejewicz

unread,
Nov 12, 2007, 5:43:19 PM11/12/07
to Google Web Toolkit
hi,

this is because of safety html strip on groups I think,

it should reads (exluding quotes!) "this.@"

regards,
Peter

Reply all
Reply to author
Forward
0 new messages