开发者

Sorting not a standard LinkedList

I have a LinkedList<Individual> where Individual is a class that has a field processorTime. It is needed to sort this LinkedList (des开发者_Python百科cending order) basing on a function estimate(processorTime) which returns integers.

Please, tell me how can I do this?


Check this guide here:

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

Quotations from the site:

The java.util.Comparator interface can be used to create objects to pass to sort methods or sorting data structures. A Comparator must define a compare function which takes two Objects and returns a -1, 0, or 1

Pasted code:

// 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());
        }
    }
}


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


Do you mean the LinkedList is not standard, or the sort order based on processor time is not standard?

If the latter, then all you need to do is use Collections.sort(list,comparator) and provide an appropriate implementation of Comparator<NodeType> for your list. See also "implementing compareTo()" which gives some good related guidance.

Post the code for your node and the desired output, and we can give you more specific direction.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜