适当的双向排序方式 [英] Proper way for two-way sorting

查看:115
本文介绍了适当的双向排序方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的java pojo,看起来像这样:

I've got a simple java pojo which looks like this:

class MyClass
{
  public String getGroup();
  public String getTitle();
}



现在我想做的是主要对MyClass pojo列表getTitle()方法返回的值。很容易,虽然使用我自己的比较器。但是,我想要的是getGroup()返回的具有相同值的实例彼此相邻。现在我做的是像

Now what I want to do is to primarily sort a list of MyClass pojo by the values returned by the getTitle() method. Easy going though using my own comparator. However, what I want is that instances with the same value returned by getGroup() being followed by each other. Now what I did was something like

.. compare(MyClass c1, MyClass c2)
{
  if (c1.getGroup().compareTo(c2.getGroup()) == 0)
  {
    return c1.getTitle().compareTo(c2.getTitle());
  }
  return c1.getGroup().compareTo(c2.getGroup());
}

但是,此代码的问题是它不再主要按标题,因为我首先比较组的内容,而不是标题,所以一个以B开头的组将会出现在以C开头的组之前,但它的标题可能先到来..什么是正确的方式主要排序

However, the issue of this code is that it is no longer primarily sorted by the title because I do first compare the content of the groups, not the title, so a group starting with "B" would come before a group starting with "C" eventhough its title may come first.. what's the proper way to primarily sort by title but make sure groups are "groupped" together as well?

示例数据:

MyClass 1 (group = "A", title="5")
MyClass 2 (group = "B", title="9")
MyClass 3 (group = "B", title="1")

使用我之前的代码会在

MyClass 1 (group = "A", title="5")
MyClass 3 (group = "B", title="1")
MyClass 2 (group = "B", title="9")

- >按组排序,然后按标题排序

-> sort by group, then sort by title

但我想

MyClass 3 (group = "B", title="1")
MyClass 2 (group = "B", title="9")
MyClass 1 (group = "A", title="5")

- >按标题排序,但确保每个相等的组这就是为什么仍然有标题5的MyClass 1在标题为9的MyClass 2之后...

-> sort by title but make sure each equal group follows each other that's why still MyClass 1 with title "5" comes after MyClass 2 with title "9"...

推荐答案

使用比较器。你需要运行排序的标题,然后对于每个未访问的标题排序相应的组。下面是一些你想要的例子代码。

Can't be done with a Comparator. You need to run through the sorted titles, then for each unvisited title sort the corresponding groups. Here's some sample code that sorts like the way you wanted.

public class MyClass {
    private String  group;
    private String  title;

    public MyClass(String g, String t) {
        group=g;
        title=t;
    }

    static Comparator<MyClass>  TITLE_COMPARATOR    = 
            new Comparator<MyClass>() {
                    @Override
                    public int compare(MyClass c1, MyClass c2) {
                            return c1.title.compareTo(c2.title);
                    }
            };
    static Comparator<MyClass>  GROUP_COMPARATOR    = new Comparator<MyClass>() {
                    @Override
                    public int compare(MyClass c1, MyClass c2) {
                            return c1.group.compareTo(c2.group);
                    }
            };

    public static List<MyClass> sublist(List<MyClass> list, String group) {
            ArrayList<MyClass> ret = new ArrayList<MyClass>();
            for (MyClass mc : list)
                if (mc.group.equals(group))
                    ret.add(mc);
            return ret;
        }

public static void main(String[] argv) {
    ArrayList<MyClass> sorted = new ArrayList<MyClass>();

    ArrayList<MyClass> list = new ArrayList<MyClass>();
    list.add(new MyClass("A", "5"));
    list.add(new MyClass("B", "9"));
    list.add(new MyClass("B", "1"));
    Collections.sort(list, TITLE_COMPARATOR);
    Hashtable<String, Boolean> visited = new Hashtable<String, Boolean>();
    for (MyClass mc : list) {
        if (visited.get(mc.group) == null) {
            List<MyClass> sublist = sublist(list, mc.group);
            Collections.sort(sublist, GROUP_COMPARATOR);
            sorted.addAll(sublist);
            visited.put(mc.group, Boolean.TRUE);
        }
    }

    for (MyClass mc : sorted)
        System.out.println(mc.group + " " + mc.title);
}

}

这篇关于适当的双向排序方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆