我创建了一个包含列表包含字符串的复合。我有一个组合框,其中有选项升序和降序。所以现在如果我在组合框中选择升序,那么列表应该按字母顺序排序。那么怎么做呢。
例如,如果我的复合材料包含一个列表"一些","零","一个" 并在我的组合框中选择升序,它应该被排序并显示为一个,一些,零。
创建组合框的代码如下
String[] ITEMS1 = {"A-Z", "Z-A" };
comboSort = new Combo(comboComposite, SWT.NONE);
comboSort.setBounds(84, 2, 91, 23);
comboSort.setItems(ITEMS1);
tabFolder = new TabFolder(topComposite, SWT.NONE);
GridData tabFolderGD = new GridData(SWT.FILL, GridData.FILL, true, true);
tabFolderGD.verticalIndent = 4;
tabFolderGD.horizontalSpan = 2;
tabFolder.setLayoutData(tabFolderGD);
TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
tabItem.setText("My created");
createListViewMycreated(tabFolder,tabItem);
tabitem和监听器的代码如下
private void createListViewMycreated(Composite composite, TabItem ItemMycreated){
List myCreatedList = new List(composite,SWT.BORDER);
myCreatedList.setItems(new String[]{"CompSetup_SEMCW8459_TiffanyA005 ","Product_Sirius_Perch_OEM0_AID1_00440245156645 ","SW_SEC Test_DEMO_Sirius "});
ItemMycreated.setControl(myCreatedList);
setDragSource(myCreatedList);
comboSort.addSelectionListener(new SelectionListener() {
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
System.out.println("hi you selected me in combo box"+comboSort.getText());
}
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
System.out.println("hi you selected me in combo box"+comboSort.getText());
String ascending =comboSort.getText();
if (ascending== "A-Z")
{
}
}
});
}
那么现在我们如何根据组合框选择对列表进行排序
答案 0 :(得分:0)
我认为你的myCreatedList有字符串列表。如果你想按升序排序吗
Collections.sort(myCreatedList);
如果它下降那么
Collections.reverse(myCreatedList);
有关馆藏的更多信息
答案 1 :(得分:0)
这可能会达到目的:
// Re-sort
String[] items = combo.getItems();
Arrays.sort(items);
combo.setItems(items);