简体   繁体   English

如何按JFace TableViewer的多列进行排序

[英]How to sort by multiple columns of JFace TableViewer

I'm trying to implement a tableviewer that can sort values depending on the order in which column header(s) are clicked by user. 我正在尝试实现一个tableviewer,它可以根据用户单击列标题的顺序对值进行排序。 eg If there are three columns column1, column2, column3 and user has clicked the columns in the following order column3, column2, column1 then, rows first on the values of column 3 gets sorted, then column 2 and finally column1. 例如,如果有三列column1,column2,column3和user按以下顺序列出了column3,column2,column1,那么第3列的值首先排序,然后是第2列,最后是column1。

I have overided the compare method in the folowing way: 我已经按照以下方式覆盖了比较方法:

protected int doCompare(Viewer tableViewer, Object row1, Object row2) 
{
    SampleTableLabelProvider labelProvider = SampleTableLabelProvider ((TableViewer) tableViewer).getLabelProvider());
    boolean areBothRowsEqual = false;
    for(String orderedColumn : getOrderedList()) // getOrderedList() stores the column names in the order in which they are clicked by user
    {
        int orderedColumnIndex = // logic to get the index of orderedColumn in the tableViewer  ;
        if(compareStrings(labelProvider, row1, row2, orderedColumnIndex ) == 0)
        {
            areBothRowsEqual = true;
        }
        else
        {
            areBothRowsEqual = false;
            break;
        }   

    }
    if(areBothRowsEqual)
    {
        return compareStrings(labelProvider, row1, row2, columnIndex);
    }
    else
    {
        return 0;
    }
}   

This logic is not working and this is what is happening: For eg column3 is selected by the user and this column gets sorted. 这个逻辑不起作用,这就是正在发生的事情:例如,用户选择了column3并且该列被排序。 But next time when a another column say column2 is clicked the objects row1 and row2 from the TableViewer don't appear in the latest ordered format. 但是下次当另一列说单击column2时,TableViewer中的对象row1和row2不会以最新的有序格式出现。

How do I achieve this? 我该如何实现这一目标?

The problem you are facing seems to be in properties of sorting algorithm. 您面临的问题似乎是排序算法的属性。 Stable algorithm will leave your date column1 sorted after you implement column2 sorting. 在实现column2排序后,稳定算法将使日期column1排序。 You have to check whether you algorithm in the method getOrderedList() is stable . 您必须检查方法getOrderedList()中的算法是否稳定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM