首先在Java中按字符排序

时间:2015-05-18 19:10:07

标签: java sorting

我需要对列表进行排序,以便首先对以char开头的所有字符串进行排序,然后按字符串以升序排序。如果不是解决这个问题的最佳方法,那么算法是否有任何构建?

$(document).ready(function(){
    if ($(window).width() < 480){
        javascript:__doPostBack('PC8365$lbtnListView','')
    }
    else {
        javascript:__doPostBack('PC8365$lbtnCalView','');
    }
});

Doctor Review
3rd Party Contact
Appointment
24-Hour Service
Doctor Preparation

我正在尝试编写自定义Comparator,如this,但不确定如何将字符串以字符和数字开头。

4 个答案:

答案 0 :(得分:1)

正如您所推测的那样,需要自定义Comparator

现在,您遇到的困难是使用ASCII集之外的字符;特别是,由于Java在内部将文本数据存储为UTF-16代码单元,因此BMP之外的任何内容都将是两个char ...

但是,让我们说这不是一个问题,你唯一关心的是&#34;好老&#34;阿拉伯数字,恰好可以用一个char表示。

然后你就可以编写这样一个比较器( UNTESTED!):

public static final Comparator<String> ARABIC_NUMBERS_LAST_COMPARATOR
    = new Comparator<String>()
    {
        @Override
        public int compare(final String o1, final String o2)
        {
            return doCompare(CharBuffer.wrap(o1), CharBuffer.wrap(o2));
        }

        private int doCompare(final CharBuffer buf1, final CharBuffer buf2)
        {
            // Test for the emptiness of each buffer
            final int r1 = buf1.remaining();
            final int r2 = buf2.remaining();
            if (r1 == 0)
                return r2 == 0 ? 0 : -1;
            if (r2 == 0)
                return 1; // we know that r1 is not empty here

            // Grab the first character from both buffers
            final char c1 = buf1.get();
            final char c2 = buf2.get();

            // If both characters are the same we must continue
            if (c1 == c2)
                return doCompare(buf1, buf2);

            // They are not... Test whether either of them is a digit
            final boolean oneIsDigit = Character.isDigit(c1);
            final boolean twoIsDigit = Character.isDigit(c2);

            // Both are digits: return what the contract expects
            if (oneIsDigit && twoIsDigit)
                return c1 - c2;

            // From this point on we know that at least one character is
            // not a digit, and that they are both different.
            if (oneIsDigit)
                return 1;
            if (twoIsDigit)
                return -1;
            // Both are not digits, but we know them to be different:
            // just return the difference
            return c1 - c2;
        }
    }
}

答案 1 :(得分:1)

这个怎么样

public class TryMe {
  public static void main(String[] args) {
    List<String> sortMe = new ArrayList<>();
    sortMe.add("Doctor Review");
    sortMe.add("3rd Party Contact");
    sortMe.add("Appointment");
    sortMe.add("24-Hour Service");
    sortMe.add("Doctor Preparation");
    Collections.sort(sortMe, new MyComparator());
    System.out.println(sortMe);
  }

  private static class MyComparator implements Comparator<String> {
    @Override
    public int compare(String o1, String o2) {
      if (Character.isDigit(o1.charAt(0))) {
        if (Character.isDigit(o2.charAt(0))) {
          //replace here with "return o1.compareTo(o2)" for original
          int i1 = getFirstDigits(o1);
          int i2 = getFirstDigits(o2);
          if (i1 == i2) return o1.compareTo(o2);
          return i1 - i2;
        } else {
          return 1;
        }
      }
      if (Character.isDigit(o2.charAt(0))) return -1;
      return o1.compareTo(o2);
    }
  }

  private static int getFirstDigits(String from) {
    int i = 0;
    for (i = 0 ; i < from.length() ; i++) {
      if (!Character.isDigit(from.charAt(i))) {
        return Integer.parseInt(from.substring(0, i));
      }
    }
    if (i > 0) return Integer.parseInt(from.substring(0, i));
    throw new IllegalArgumentException("No digits to parse, should not happen");
  }
}

答案 2 :(得分:0)

您可以定义自己的RuleBasedCollator(请参阅下面的代码中的myrules)。

此方法适用于第一个位置之后的数字!

    List<String> list = new ArrayList<String>();
    list.add("Doctor Review");
    list.add("Doctor Review A"); // check sort order of this entry!
    list.add("Doctor Review 1"); // check sort order of this entry!
    list.add("3rd Party Contact");
    list.add("Appointment");
    list.add("24-Hour Service");
    list.add("Doctor Preparation");

    String myrules = "< a, A < b, B < c, C < d, D < e, E < f, F < g, G < h, H < i, I" +
        "< j, J < k, K < l, L < m, M < n, N < o, O < p, P < q, Q < r, R" +
        "< s, S < t, T < u, U < v, V < w, W < x, X < y, Y < z, Z" +
        "< 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9";

    try {
      RuleBasedCollator myRuleBasedCollator = new RuleBasedCollator(myrules);
      Collections.sort(list, myRuleBasedCollator);
      for (String s : list) {
        System.out.println(s);
      }
    } catch (ParseException e) {
      e.printStackTrace();
    }

输出:

Appointment
Doctor Preparation
Doctor Review
Doctor Review A
Doctor Review 1
24-Hour Service
3rd Party Contact

答案 3 :(得分:0)

   ArrayList<String> list = new ArrayList<String>();
    list.add("Doctor Review");
    list.add("3rd Party Contact");
    list.add("Appointment");
    list.add("24-Hour Service");
    list.add("Doctor Preparation");
    ArrayList<String> newlist = new ArrayList<>();
    Collections.sort(list);
    for (String x : list)

    {
        if (!Character.isDigit(x.charAt(0))) {
            newlist.add(x);
        }
    }

    for (String x : list)

    {
        if (Character.isDigit(x.charAt(0))) {
            newlist.add(x);
        }
    }

解决方案,使用其他列表。