Java 使用可比数据进行排序
简介 我使用Comparable进行自定义排序的代码并没有按照我希望的方式工作。我基本上是获取一个目录数组并按以下方式进行排序:Java 使用可比数据进行排序,java,sorting,comparable,Java,Sorting,Comparable,简介 我使用Comparable进行自定义排序的代码并没有按照我希望的方式工作。我基本上是获取一个目录数组并按以下方式进行排序: 目录的数量越少,排在第一位的目录就越少 如果按字母顺序打成平局 问题 您可以使用的输入示例如下: [“/”、“/usr/”、“/usr/local/”、“/usr/local/bin/”、“/games/”, “/games/snake/”、“/homography/”、“/temp/downloads/”] 应返回以下内容: [“/”、“/games/”、“/hom
import java.util.*;
public class Dirsort { public String[] sort(String[] dirs) {
//Creates Array list containing Sort object
ArrayList<Sort> mySort = new ArrayList<Sort>();
//Loop that gets the 3 needed values for sorting
for (String d: dirs){
String [] l = d.split("/");//String array for alphabetical comparison
int di = d.length();//Length of array for sorting by number of directories
mySort.add(new Sort(di,l,d));//adds Sort object to arraylist (note d (the entire directory) is needed for the toString)
}
Collections.sort(mySort);//sorts according to compareTo
String [] ans = new String [mySort.size()];//Creates a new string array that will be returned
int count = 0;//to keep track of where we are in the loop for appending
for (Sort s: mySort){
ans[count] = s.toString();
count++;
}
return ans;
}
class Sort implements Comparable<Sort>{
private int d;//number of directories
private String [] arr;//array of strings of names of directories
private String dir;//full directory as string for toString
//Constructor
public Sort(int myD, String [] myArr, String myDir){
d = myD;
arr = myArr;
dir = myDir;
}
//toString
public String toString(){
return dir;
}
@Override
public int compareTo(Sort arg0) {
// TODO Auto-generated method stub
//If they are the same return 0
if (this.equals(arg0)){
return 0;
}
//if the directories are empty
if("/".equals(arg0.dir)){
return 1;
}
if ("/".equals(this.dir)){
return -1;
}
//If they are not the same length the shorter one comes first
if (this.d != arg0.d){
return this.d - arg0.d;
}
//If they are the same length, compare them alphabetically
else{
for (int i = 0; i < arg0.d; i++){
if (!this.arr[i].equals(arg0.arr[i])){
return this.arr[i].compareTo(arg0.arr[i]);
}
}
}
return 0;
}
}
}
import java.util.*;
公共类Dirsort{public String[]sort(String[]dirs){
//创建包含排序对象的数组列表
ArrayList mySort=新的ArrayList();
//获取排序所需的3个值的循环
for(字符串d:dirs){
String[]l=d.split(“/”;//用于字母顺序比较的字符串数组
int di=d.length();//按目录数排序的数组长度
添加(新排序(di,l,d));//将排序对象添加到arraylist(注意,toString需要d(整个目录))
}
Collections.sort(mySort);//根据compareTo进行排序
String[]ans=new String[mySort.size()];//创建一个将返回的新字符串数组
int count=0;//跟踪我们在循环中的附加位置
for(排序s:mySort){
ans[count]=s.toString();
计数++;
}
返回ans;
}
类排序实现了可比较的{
private int d;//目录数
私有字符串[]arr;//目录名称字符串数组
private String dir;//作为toString字符串的完整目录
//建造师
公共排序(int-myD,String[]myArr,String-myDir){
d=myD;
arr=myArr;
dir=myDir;
}
//托斯特林
公共字符串toString(){
返回目录;
}
@凌驾
公共整数比较(排序arg0){
//TODO自动生成的方法存根
//如果它们相同,则返回0
if(this.equals(arg0)){
返回0;
}
//如果目录是空的
如果(“/”.equals(arg0.dir)){
返回1;
}
如果(“/”.equals(this.dir)){
返回-1;
}
//如果它们的长度不相同,则以较短的为准
if(this.d!=arg0.d){
返回此.d-arg0.d;
}
//如果长度相同,请按字母顺序进行比较
否则{
for(int i=0;i
错误在这里:
for (String d: dirs){
String [] l = d.split("/");
int di = d.length(); // <- here
mySort.add(new Sort(di,l,d));
}
我相信你想要的是这个,用分割的长度:
int di = l.length;
那么输出是:
/
/games/
/homework/
/usr/
/games/snake/
/temp/downloads/
/usr/local/
/usr/local/bin/
所以你可能想做点什么。虽然在这里,这意味着它们都是以空字符串开始的,所以它不会对您进行比较的方式产生影响
另一方面,@JBNizet建议给变量起更多有意义的名字,这在这里也很有帮助
fullDir.length()
和splitDir.length
会使这一点更容易发现(而且可能在一开始就从未发生过)。下面是一个固定版本的代码,它处理两个目录都是“/”
的情况,它删除了不必要且传递错误的零件数组长度,使用更有意义的变量名:
public class Dirsort {
public static void main(String[] args) {
String[] input = new String[] {
"/",
"/usr/",
"/usr/local/",
"/usr/local/bin/",
"/games/",
"/games/snake/",
"/homework/",
"/temp/downloads/"
};
String[] result = new Dirsort().sort(input);
System.out.println("result = " + Arrays.toString(result));
}
public String[] sort(String[] dirs) {
ArrayList<Sort> sorts = new ArrayList<Sort>();
for (String dir : dirs) {
String[] parts = dir.split("/");
sorts.add(new Sort(parts, dir));
}
Collections.sort(sorts);
String[] result = new String[sorts.size()];
int count = 0;
for (Sort sort: sorts) {
result[count] = sort.toString();
count++;
}
return result;
}
class Sort implements Comparable<Sort> {
private String[] parts;
private String dir;
public Sort(String[] parts, String dir) {
this.parts = parts;
this.dir = dir;
}
public String toString(){
return dir;
}
@Override
public int compareTo(Sort other) {
if (this.equals(other)){
return 0;
}
if("/".equals(other.dir) && "/".equals(dir)) {
return 0;
}
if("/".equals(other.dir)){
return 1;
}
if ("/".equals(this.dir)){
return -1;
}
if (this.parts.length != other.parts.length){
return this.parts.length - other.parts.length;
}
else {
for (int i = 0; i < other.parts.length; i++){
if (!this.parts[i].equals(other.parts[i])){
return this.parts[i].compareTo(other.parts[i]);
}
}
}
return 0;
}
}
}
公共类目录排序{
公共静态void main(字符串[]args){
字符串[]输入=新字符串[]{
"/",
“/usr/”,
“/usr/local/”,
“/usr/local/bin/”,
“/游戏/”,
“/games/snake/”,
“/家庭作业/”,
“/temp/downloads/”
};
String[]result=new Dirsort().sort(输入);
System.out.println(“result=“+Arrays.toString(result));
}
公共字符串[]排序(字符串[]目录){
ArrayList排序=新建ArrayList();
for(字符串目录:目录){
String[]parts=dir.split(“/”);
添加(新排序(部件,目录));
}
集合。排序(排序);
字符串[]结果=新字符串[sorts.size()];
整数计数=0;
for(排序:排序){
结果[计数]=sort.toString();
计数++;
}
返回结果;
}
类排序实现了可比较的{
私有字符串[]部分;
私有字符串目录;
公共排序(字符串[]部分,字符串目录){
这一部分=部分;
this.dir=dir;
}
公共字符串toString(){
返回目录;
}
@凌驾
公共整数比较(排序其他){
如果(这个等于(其他)){
返回0;
}
如果(“/”.equals(other.dir)和(&“/”.equals(dir)){
返回0;
}
如果(“/”.equals(other.dir)){
返回1;
}
如果(“/”.equals(this.dir)){
返回-1;
}
if(this.parts.length!=其他.parts.length){
返回this.parts.length-other.parts.length;
}
否则{
for(int i=0;i"/usr/".split("/") == { "", "usr" }
public class Dirsort {
public static void main(String[] args) {
String[] input = new String[] {
"/",
"/usr/",
"/usr/local/",
"/usr/local/bin/",
"/games/",
"/games/snake/",
"/homework/",
"/temp/downloads/"
};
String[] result = new Dirsort().sort(input);
System.out.println("result = " + Arrays.toString(result));
}
public String[] sort(String[] dirs) {
ArrayList<Sort> sorts = new ArrayList<Sort>();
for (String dir : dirs) {
String[] parts = dir.split("/");
sorts.add(new Sort(parts, dir));
}
Collections.sort(sorts);
String[] result = new String[sorts.size()];
int count = 0;
for (Sort sort: sorts) {
result[count] = sort.toString();
count++;
}
return result;
}
class Sort implements Comparable<Sort> {
private String[] parts;
private String dir;
public Sort(String[] parts, String dir) {
this.parts = parts;
this.dir = dir;
}
public String toString(){
return dir;
}
@Override
public int compareTo(Sort other) {
if (this.equals(other)){
return 0;
}
if("/".equals(other.dir) && "/".equals(dir)) {
return 0;
}
if("/".equals(other.dir)){
return 1;
}
if ("/".equals(this.dir)){
return -1;
}
if (this.parts.length != other.parts.length){
return this.parts.length - other.parts.length;
}
else {
for (int i = 0; i < other.parts.length; i++){
if (!this.parts[i].equals(other.parts[i])){
return this.parts[i].compareTo(other.parts[i]);
}
}
}
return 0;
}
}
}
public class Disort
{
public static String[] sort(String[] dirs)
{
ArrayList<Path> mySort = new ArrayList<Path>();
Path pathDir;
for(String dir : dirs){
pathDir = Paths.get(dir);
// check if directory exists
if(Files.isDirectory(pathDir)){
mySort.add(pathDir);
}
}
// sort the ArrayList according a personalized comparator
Collections.sort(mySort, new Comparator<Path>(){
@Override
public int compare(Path o1, Path o2)
{
if(o1.getNameCount() < o2.getNameCount()){
return -1;
}
else if(o1.getNameCount() > o2.getNameCount()){
return 1;
}
else{
return o1.compareTo(o2);
}
}
});
// to return a String[] but it will better to return a ArrayList<Path>
String[] result = new String[mySort.size()];
for(int i = 0; i < result.length; i++){
result[i] = mySort.get(i).toString();
}
return result;
}
}