简体   繁体   English

使用ECollections进行Xtend排序

[英]Xtend sorting using ECollections

I am trying to sort an EList of functions by using Java's Comparator, but the list is not sorted after calling sort(). 我正在尝试使用Java的Comparator对函数的EList进行排序,但是在调用sort()之后,该列表未进行排序。

The way I implement it is this: 我实现它的方式是这样的:

//Func.xtend <-- Start -->

public class NameComparator implements Comparator<Function_Name> {  
    override int compare (Function_Name function1, Function_Name function2)
    {
        return function1.func.compareToIgnoreCase(function2.func)
    }
}

public class CheckNames {   
    def void checkDuplicateNames(Main_func para_func) {
        var EList<Function_Name> functions = para_func.getContains()        
        var NameComparator pc = new NameComparator()

        functions.sort(pc) //<-- sorting here

        var Iterator<Function_Name> oIt = functions.iterator ()
        while (oIt.hasNext)
        {           
            var Function_Name func = oIt.next   ()
            System::out.println(func.func.toString)
        }
    }
}

// <-- End -->

Am I doing something wrong? 难道我做错了什么? Theoretically, after calling functions.sort(pc), the contents in variable "functions" should already be sorted, right? 从理论上讲,在调用functions.sort(pc)之后,变量“ functions”中的内容应该已经排序了,对吗? Or do I still need to do some processing? 还是我仍然需要做一些处理?

As far as I know, List::sort in Xtend doesn't sort in-place, but returns a new sorted list. 据我所知,Xtend中的List::sort不会就地排序,但是会返回一个新的排序列表。 So you have to call List::sortInPlace instead. 因此,您必须改为调用List::sortInPlace

Apart from this, you might run into problems when you try to sort unique ELists (for example, containments) in-place. 除此之外,尝试就地对唯一的 ELists(例如,收容物)进行排序时,您可能会遇到问题。 During the sort operation (Xtend actually calls java.util.Collections.sort for this), some elements may temporarily be contained twice in the list. 在排序操作期间(Xtend为此实际上调用了java.util.Collections.sort ),某些元素可能会在列表中暂时包含两次。 However, unique lists check for duplicates and throw an IllegalArgumentException when you try to add an element a second time (see AbstractEList.set(in, E) ). 但是,当您尝试第二次添加元素时,唯一列表会检查重复项并抛出IllegalArgumentException (请参见AbstractEList.set(in, E) )。 To avoid this, you should sort unique ELists like this: 为了避免这种情况,您应该像这样对独特的ELists进行排序:

var sortedFunctions = functions.sort(pc)
functions.clear()
functions.addAll(sortedFunctions)

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

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