排序不是标准的LinkedList

时间:2010-10-25 17:22:01

标签: java

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

请告诉我该怎么做?

3 个答案:

答案 0 :(得分: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());
        }
    }
}

答案 1 :(得分: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
  }
});

答案 2 :(得分:2)

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