2012-08-10 83 views
2
private class FileType extends Object { 
    private String Name; 
    private String Type; 

    public FileType() { 
     Name = null; 
     Type = null; 
    } 

    public void setFileType(String n, String t) { 
     Name = n; 
     Type = t; 
    } 

    public int compareTo(FileType ft) { 
     String decodedFileName = null; 
     String decodedInputName = null; 
     try { 
      decodedFileName = URLDecoder.decode(this.Name, "UTF-8"); 
      decodedInputName = URLDecoder.decode(ft.Name, "UTF-8"); 
     } 
     catch(UnsupportedEncodingException e) { 
     } 
     return decodedFileName.compareToIgnoreCase(decodedInputName); 
    } 
} 

上面的代码是我定义文件列表的类。
我已经实现了比较文件名。
该类型可能为FolderFile
但我想排序文件的第一个优先级是Type,第二个优先级是Name。
如何才能到达它?如何实现排序类

+5

的防止'延伸Object'是多余的,防止合法的传承。 – adarshr 2012-08-10 09:23:27

+1

我认为你应该实现可比的界面。为什么扩展对象是隐式需要的? – 2012-08-10 09:24:13

+0

这是功课吗? – Elchin 2012-08-10 09:25:35

回答

1
if (this.Type.equals(ft.Type)){ 
    return decodedFileName.compareTo(decodedInputName); 
} 
else{ 
    return this.Type.compareTo(ft.Type); 

} 
3

你必须实现Comparable/compareTo方法。

2

使用Comparator接口从java.util package排序的方式不止一种......

用比较的方法compare(T t1, T t2) 如:

下面是来自现场的例子http://www.mkyong.com

import java.util.Comparator; 

public class Fruit{ 

    private String fruitName; 
    private String fruitDesc; 
    private int quantity; 

    public Fruit(String fruitName, String fruitDesc, int quantity) { 
     super(); 
     this.fruitName = fruitName; 
     this.fruitDesc = fruitDesc; 
     this.quantity = quantity; 
    } 

    public String getFruitName() { 
     return fruitName; 
    } 
    public void setFruitName(String fruitName) { 
     this.fruitName = fruitName; 
    } 
    public String getFruitDesc() { 
     return fruitDesc; 
    } 
    public void setFruitDesc(String fruitDesc) { 
     this.fruitDesc = fruitDesc; 
    } 
    public int getQuantity() { 
     return quantity; 
    } 
    public void setQuantity(int quantity) { 
     this.quantity = quantity; 
    } 



    public static Comparator<Fruit> FruitNameComparator 
          = new Comparator<Fruit>() { 

     public int compare(Fruit fruit1, Fruit fruit2) { 

      String fruitName1 = fruit1.getFruitName().toUpperCase(); 
      String fruitName2 = fruit2.getFruitName().toUpperCase(); 

      //ascending order 
      return fruitName1.compareTo(fruitName2); 

      //descending order 
      //return fruitName2.compareTo(fruitName1); 
     } 

    }; 
} 
2

比较两种类型。如果比较结果与0不同,则返回结果。如果等于0,则比较名称。

需要注意的是:

  • 扩展对象是不必要的:它是默认
  • 字段应该以小写字母开头:名称,类型呐dnot名称,类型
  • 你的类应该实现Comparable<FileType>
  • 我会为这个类别选择另一个名称:它不是文件类型,而是与文件类型关联的文件名
  • 我将使用枚举而不是字符串作为文件类型,因为你只有两个有效的文件类型实例
  • 你永远不应该忽略你正在做的异常。顺便说一句,如果发生这种异常,它可能会导致NullPointerException。包装该异常进入运行异常,并抛出此运行时异常:

    catch(UnsupportedEncodingException e) { 
        throw new RuntimeException(e); 
    } 
    
  • 你compareTo方法不处理空的名字,虽然默认的构造函数赋值为null的名称。修复方法或构造函数。在我看来,文件名不应该为空,所以我会修复构造函数。
1

您首先比较类型和解码名称。 我直接在类中缓存decodeFileName值以防止调用URLDecoder.decode过多。

private class FileType extends Object implements Comparable<FileType>{ 
    private String name; 
    private String decodedFileName; 
    private String type; 
    public FileType(String n, String t) { 
     name = n; 
     try { 
      decodedFileName = URLDecoder.decode(this.name, "UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      throw new RuntimeException(e); 
     }   
     type = t; 
    } 
    public int compareTo(FileType other) { 
     int result = type.compareToIgnoreCase(other.type); 
     if (result == 0){ 
      result = decodedFileName.compareToIgnoreCase(other.decodedFileName); 
     } 
     return result; 
    } 
} 
1

如果你只有两种类型,为什么不让它们枚举?
那么首先你比较type.ordinal,如果有相等,则比较名称, ,也从投入不需要的值有