2011-03-14 28 views
0

我有以下代码进行排序。这可以改善吗?排序功能 - 这怎么可以改进

import java.util.*; 
class Church { 
    private String name; 
    private String pastor; 
    public Church(String name, String pastor) { 
     this.name = name; 
     this.pastor = pastor; 
    } 
    public String getPastor() { 
     return pastor; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setPastor(String pastor) { 
     this.pastor = pastor; 
    } 
    public String toString() { 
     return getName() + " is Pastored by "+getPastor(); 
    } 
    public int compareByPastor(Church c) { 
     int x = pastor.compareTo(c.getPastor()); 
     return x; 
    } 
    public int compareByName(Church c) { 
     int x = name.compareTo(c.getName()); 
     return x; 
    } 
} 

class Churches { 
    private final List<Church> churches; 

    public Churches() { 
     churches = new ArrayList<Church>(); 
    } 
    public void addWithoutSorting(Church c) { 
     churches.add(c); 
    } 

    //You could always add using this method 
    public void addWithSorting(Church c) { 

    } 
    public void display() { 
     for(int j = 0; j < churches.size(); j++) { 
      System.out.print(churches.get(j).toString()); 
      System.out.println(""); 
     } 
    } 
    public List<Church> getChurches() { 
     return churches; 
    } 
    public void sortBy(String s) { 
     for (int i = 1; i < churches.size(); i++) { 
      int j; 
      Church val = churches.get(i); 
      for (j = i-1; j > -1; j--) { 
       Church temp = churches.get(j); 
       if(s.equals("Pastor")) { 
        if (temp.compareByPastor(val) <= 0) { 
         break; 
        } 
       } 
       else if(s.equals("Name")) { 
        if (temp.compareByName(val) <= 0) { 
          break; 
        } 
       } 
       churches.set(j+1, temp); 
      } 
      churches.set(j+1, val); 
     } 
    } 

    public static void main(String[] args) { 
     Churches baptists = new Churches(); 
     baptists.addWithoutSorting(new Church("Pac", "Pastor G")); 
     baptists.addWithoutSorting(new Church("New Life", "Tudor")); 
     baptists.addWithoutSorting(new Church("My Church", "r035198x")); 
     baptists.addWithoutSorting(new Church("AFM", "Cathy")); 
     System.out.println("**********************Before Sorting***********************"); 
     baptists.display(); 
     baptists.sortBy("Pastor"); 
     System.out.println("**********************After sorting by Pastor**************"); 
     baptists.display(); 
     baptists.sortBy("Name"); 
     System.out.println("**********************After sorting by Name****************"); 
     baptists.display(); 

    } 

    } 
+0

从这个问题来看,你不清楚你想如何分类教堂:按姓名,按牧师的名字?按会众的大小?通过尖顶高度? – rtperson

回答

3

在Collections.sort(名单时,比较器) http://download.oracle.com/javase/6/docs/api/java/util/Collections.html

+2

编写两个自定义比较器:一个按牧师排序,另一个按名称排序。当你想由牧师排序时,调用'Collections.sort(yourList,pastorComparator)'。当你想按名称排序时,调用'Collections.sort(yourList,nameComparator)'。 – mre

0
class Churches 
{ 
    public void sortBy(String attribute) 
    { 
     Comparator<Church> c = null; 

     if ("Name".equals(attribute)) c = new ChurchNameComparator(); 
     else if ("Pastor".equals(attribute)) c = new ChurchNameComparator(); 
     else System.out.println("unexpected sort attribute : '" + attribute + "'"); 

     if (c != null) Collections.sort(churches, c); 
    } 

    private static final class ChurchNameComparator implements Comparator<Church> 
    { 
     public int compare(Church c1, Church c2) 
     { 
     return c1.getName().compareTo(c2.getName()); 
     } 
    } 

    private static final class ChurchPastorComparator implements Comparator<Church> 
    { 
     public int compare(Church c1, Church c2) 
     { 
     return c1.getPastor().compareTo(c2.getPastor()); 
     } 
    } 
} 
0

这里真正的答案看看是线几乎与iluxa的:你要实现你的教会比较接口对象(示例代码here,虽然你会想要决定什么构成比教堂更大/更小......),然后你可以使用Collections.sort()对它们进行排序。这将在一天结束时完成工作。

当然,你刚才问的意见有关堆栈溢出排序,所以我觉得有必要问你是否需要一个就地排序,你要找什么样的大O性能,然后要求您在Quicksort,IntroSort,HeapSort,MergeSort和StoogeSort之间进行选择,以便最适合您的项目。

踢,我曾经编写了几个种类在Java中:

  • This one军队快速排序到二次的时间,这是很难做的比我原来设想,
  • 这一个展示如何实施MergeSort
  • 而这一次演示了HeapSort

我做这些我自己的享受和教育。作为一般规则,您希望坚持使用标准库来处理这些事情。