0

Say I have an array of strings:

String[] array = {
   "2183417234 somerandomtexthere",
   "1234123656 somemorerandomtexthere",
   "1093241066 andevenmore",
   "1243981234 you get what i mean",
   //etc

};

How would I sort this array using the long (it's a long) at the start of the string, so it'll end up looking like this:

String[] array = {
   "1093241066 andevenmore",
   "1234123656 somemorerandomtexthere",
   "1243981234 you get what i mean",
   "2183417234 somerandomtexthere",
   //etc

};

I've tried everyting from making it an arraylist and using Collections#sort to creating my own comparator, to using a sorted map / tree map and I just can't figure it out. Thanks.

4

4 回答 4

2

使用这个功能:

static long comparedValue(String s) {
  return Long.valueOf(s.substring(0, s.indexOf(' ')));
}

然后根据它定义一个比较器:

public int compare(String left, String right) {
  return comparedValue(left) - comparedValue(right);
}
于 2012-07-31T21:34:44.393 回答
1

使用谷歌番石榴

List<String> unsorted = Arrays.asList(array);

Function<String, Long> longFunction = new Function<String, Long>() {
  @Override public Long apply(String input) {
    return Long.valueOf(input.split(" ")[0]);
  }
};

List<String> sorted = Ordering.natural().onResultOf(longFunction).immutableSortedCopy(unsorted);

或者,如果您不想使用列表(您应该始终更喜欢集合而不是数组):

Arrays.sort(array, Ordering.natural().onResultOf(longFunction));
于 2012-07-31T21:35:35.377 回答
0

自定义比较器应该可以正常工作:

public class LongPrefixComparator implements Comparator<String> {
     @Override
     public int compare(String s1, String s2) {
         final long pref1 = getPrefixValue(s1);
         final long pref2 = getPrefixValue(s2);
         return s1 == s2 ? 0 : s1 < s2 ? -1 : 1;
     }

     private static long getPrefixValue(String stg) {
         int len = stg.indexOf(' ');
         if (len > 0) {
             try {
                 return Long.parseLong(stg.substring(0, len));
             catch (NumberFormatException ignored) {}
         }
         return 0L;
     }
}
于 2012-07-31T22:38:54.163 回答
0

您显示的输入非常好。但那是因为它们都有相同的位数。

public static void main(String[] args) {
    String[] array = { "2183417234 somerandomtexthere",
            "1234123656 somemorerandomtexthere", "1093241066 andevenmore",
            "1243981234 you get what i mean", "999 little shorter"
    // etc

    };

    List<String> list = Arrays.asList(array);
    Collections.sort(list);

    System.out.println(list);
}

当您使用一些较短的数字时,问题开始出现 - 如上图所示的 999...

输出将是:

[1093241066 andevenmore, 1234123656 somemorerandomtexthere, 1243981234 you get what i mean, 2183417234 somerandomtexthere, 999 little shorter]

所以,为了让它一直工作——你需要你的自定义比较器,它能够分割给定的字符串,然后从它们中取出数字部分,然后比较它们。使用@Marko Topolik 解决方案:

static long comparedValue(String s) {
    return Long.valueOf(s.substring(0, s.indexOf(' ')));
}

public int compare(String left, String right) {

    long result = comparedValue(left) - comparedValue(right);

    boolean numberPartAreEqual = result == 0;
    if (numberPartAreEqual) {
        result = left.compareTo(right);
    }

    return (int) result;
}
于 2012-07-31T21:45:44.007 回答