首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >排序功能-如何改进

排序功能-如何改进
EN

Stack Overflow用户
提问于 2011-03-15 03:05:07
回答 3查看 145关注 0票数 0

我有以下排序代码。这还能改进吗?

代码语言:javascript
复制
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();

    }

  }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-15 03:07:46

看看Collections.sort(列表,比较器) http://download.oracle.com/javase/6/docs/api/java/util/Collections.html

票数 3
EN

Stack Overflow用户

发布于 2011-03-15 03:23:21

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2011-03-15 03:27:04

这里的实际答案与iluxa的非常一致:您希望在您的教堂对象上实现一个比较器接口(示例代码here,尽管您可能希望确定教堂的大于/小于……),然后可以使用Collections.sort()对它们进行排序。这将在一天结束的时候完成这项工作。

当然,您刚刚询问了关于Stack Overflow排序的建议,所以我觉得有必要问您是否需要就地排序,您希望获得什么样的大O性能,然后让您在Quicksort、IntroSort、HeapSort、MergeSort和StoogeSort中选择最适合您的。

为了提高效率,我曾经用Java编写了一些排序:

  • This one强制将快速排序转换为二次时间,这比我最初假设的更难,
  • 这篇文章展示了如何实现MergeSort
  • ,这篇文章演示了一个HeapSort

我这样做是为了我自己的乐趣和教育。一般来说,对于这类事情,您应该坚持使用标准库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5303171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档