如何对ComboBox项进行排序

San*_*age 1 java sorting swing combobox vaadin

我想在我的vaadin组合框中以升序方式列出已排序的项目列表.我正在添加如下项目.

     for (long i = 1; i < 11; i++) {
            Long item = new Long(i);
            comboBoxPriority.addItem(item);

        }
Run Code Online (Sandbox Code Playgroud)

我也尝试过以下方式.我仍然按降序获得项目列表.

for (long i = 10; i > 0; i--) {
                Long item = new Long(i);
                comboBoxPriority.addItem(item);

            }
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 8

You could simply add the values to a List and uses the Collections API to sort it.

List<Long> values = new ArrayList<Long>(10);
for (long i = 10; i > 0; i--) {
    values.add(i);
}
Collections.sort(values);
DefaultComboBoxModel model = new DefaultComboBoxModel(values.toArray(new Long[values.size()]));
comboBoxPriority.setModel(model);
Run Code Online (Sandbox Code Playgroud)

You could achieve the same thing using an array and Arrays.sort if that was eaiser