gpt4 book ai didi

java - 排序不是标准的 LinkedList

转载 作者:行者123 更新时间:2023-12-01 07:15:29 27 4
gpt4 key购买 nike

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

请告诉我该怎么做?

最佳答案

在此处查看本指南:

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

引自网站:

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

粘贴代码:

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

关于java - 排序不是标准的 LinkedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4017152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com