2010-10-25 67 views
0

我有一个LinkedList<Individual>其中Individual是一个类有一个字段processorTime。 需要根据返回整数的函数estimate(processorTime)对此LinkedList进行排序(降序)。排序不是一个标准LinkedList

请告诉我该怎么做?

+0

哪里的估计方法驻留?在个人对象内部?对我来说,使用链表是明智的,因为你显然不想保留插入对象的顺序。 – mezzie 2010-10-25 17:26:39

回答

4

查阅本指南的位置:从站点

http://leepoint.net/notes-java/data/collections/comparators.html

语录:

的了java.util.Comparator接口可用于创建对象传递给排序方法或 排序数据结构。比较器必须定义一个比较功能采用两个 对象,并返回一个-1,0或1

粘贴代码:

// File: arrays/filelist/Filelistsort.java 
// Purpose: List contents of user home directory. 
//   Demonstrates use of Comparators to sort the 
//   same array by two different criteria. 
// Author: Fred Swartz 2006-Aug-23 Public domain. 

import java.util.Arrays; 
import java.util.Comparator; 
import java.io.*; 

public class Filelistsort { 

    //======================================================= main 
    public static void main(String[] args) { 
     //... Create comparators for sorting. 
     Comparator<File> byDirThenAlpha = new DirAlphaComparator(); 
     Comparator<File> byNameLength = new NameLengthComparator(); 

     //... Create a File object for user directory. 
     File dir = new File(System.getProperty("user.home")); 
     File[] children = dir.listFiles(); 

     System.out.println("Files by directory, then alphabetical"); 
     Arrays.sort(children, byDirThenAlpha); 
     printFileNames(children); 

     System.out.println("Files by length of name (long first)"); 
     Arrays.sort(children, byNameLength); 
     printFileNames(children); 
    } 

    //============================================= printFileNames 
    private static void printFileNames(File[] fa){ 
     for (File oneEntry : fa) { 
      System.out.println(" " + oneEntry.getName()); 
     } 
    } 
} 


////////////////////////////////////////////////// DirAlphaComparator 
// To sort directories before files, then alphabetically. 
class DirAlphaComparator implements Comparator<File> { 

    // Comparator interface requires defining compare method. 
    public int compare(File filea, File fileb) { 
     //... Sort directories before files, 
     // otherwise alphabetical ignoring case. 
     if (filea.isDirectory() && !fileb.isDirectory()) { 
      return -1; 

     } else if (!filea.isDirectory() && fileb.isDirectory()) { 
      return 1; 

     } else { 
      return filea.getName().compareToIgnoreCase(fileb.getName()); 
     } 
    } 
} 


////////////////////////////////////////////////// NameLengthComparator 
// To sort by length of file/directory name (longest first). 
class NameLengthComparator implements Comparator<File> { 

    // Comparator interface requires defining compare method. 
    public int compare(File filea, File fileb) { 
     int comp = fileb.getName().length() - filea.getName().length(); 
     if (comp != 0) { 
      //... If different lengths, we're done. 
      return comp; 
     } else { 
      //... If equal lengths, sort alphabetically. 
      return filea.getName().compareToIgnoreCase(fileb.getName()); 
     } 
    } 
} 
+0

非常感谢! – Dmitry 2010-10-25 17:28:20

+0

@robbrit,所以最好的做法是引用相关材料以避免需要遵循(对于以后的读者来说可能是陈旧的)链接:http://meta.stackoverflow.com/questions/7656/how-do-i-write-一个很好的回答问题。另见@Jon Skeet的博客上的广泛讨论:http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx – andersoj 2010-10-25 17:42:02

+0

啊,对不起,我是一个所以新手!我将编辑答案以包含它。 – robbrit 2010-10-25 17:43:46

2

是不是意味着LinkedList不是标准配置,或者基于处理器时间的排序顺序不是标准配置?

如果是后者,那么您所需要做的就是使用Collections.sort(list,comparator)并为您的列表提供适当的Comparator<NodeType>实现。另见"implementing compareTo()",它提供了一些很好的相关指导。

发布您的节点的代码和所需的输出,我们可以给你更具体的方向。

Collections.sort(list,new Comparator<Individual>() { 
    @Override 
    public int compare(final Individual i1, final Individual i2) { 
    return i1.processorTime - i2.processorTime; 
    } 

    @Override 
    public boolean equals(Object foo) { 
    return false; // doesn't matter, but false is better 
    } 
}); 
+0

我的意思是我的LinkedList不是,例如LinkedList 。我对比较者一无所知。现在开始学习=) – Dmitry 2010-10-25 17:27:21

+0

非常感谢! – Dmitry 2010-10-25 17:28:45

+1

箱子关了啊! – Sid 2010-10-25 17:29:34

2
Collections.sort(linkedList, new Comparator<Individual>() { 
    int compare(Individual i1, Individual i2) { 
     ... 
    } 
    }); 
+0

谢谢!我懂了 – Dmitry 2010-10-25 17:40:13