如何通过元素的一个字段的值对java中的链表进行排序?

Dav*_*vid 2 java linked-list

我在我的java程序中使用了一个链表,该元素是一个自定义类型,它有三个字段,其中一个是Integer类型.我的问题是:如何根据整数提交的值对链表进行排序?

tfk*_*tfk 8

您可以将Collections.sort方法与自定义Comparator一起使用.

Collections.sort(your_list, new Comparator<YoureValueType>(){
   @Override
   public int compare(YoureValueType o1, YoureValueType o2){
        if(o1.getMagicInt() < o2.getMagicInt()){
           return -1; 
        }
        if(o1.getMagicInt() > o2.getMagicInt()){
           return 1; 
        }
        return 0;
   }
}); 
Run Code Online (Sandbox Code Playgroud)

编辑:我刚看到亚历山大评论waldheinz答案的非常大和小的价值.我更新了我的代码以反映他的论点.