sorting strings in Java based on substrings

只谈情不闲聊 提交于 2021-02-07 18:43:44

问题


I have a list of strings def123, abc999, zzz000, abc123, zzz111. I want the list sorted such that first three characters are sorted in ascendng order and next three in descending. So the output should be abc999, abc123, def123, zzz111,zzz000 Is this possible?


回答1:


Other answers have suggested you implement Comparator. That's no longer necessary with recent utility methods added to the interface in Java 8:

list.sort(Comparator
    .comparing(s -> s.substring(0, 3))
    .thenComparing(s -> s.subtring(3, 6), Comparator.reverseOrder()));

Also note that List now has a sort method.




回答2:


Yes, it is possible. You will have to write your own Comparator. Here's a tutorial to get you started https://www.tutorialspoint.com//java/java_using_comparator.htm




回答3:


Break the strings into two substrings and then sort using comparator. Something like this: Demo

List<String> list = Arrays.asList("def123", "abc999", "zzz000", "abc123", "zzz111");

Comparator<String> cmp = new Comparator<String>() {
  public int compare(String o1, String o2) {
    int diff = (o1.substring(0,3)).compareTo(o2.substring(0,3));
        return (diff == 0) ? (Integer.valueOf(o2.substring(3)).compareTo(Integer.valueOf(o1.substring(3)))): diff;
  }
};
Collections.sort(list, cmp);



回答4:


You should create a custom class including your String as an attribute. That class should implement the Comparable interface.

Inside your custom class you have to override method compareTo(T o) which was inherited from Comparable interface. You should apply your sorting logic there.

Then if you make a Collection (ex: List<CustomClass>) using that custom class as the object type, you can use Collections.sort(list) method to sort that List.

A complete example can be found here.




回答5:


If you want more of an algorithmic approach, check this out: https://jsfiddle.net/5adpqgrc/1/

// start with ['def123', 'abc999', 'zzz000', 'abc123', 'zzz111']
var strings = ['def123', 'abc999', 'zzz000', 'abc123', 'zzz111']
$('body').append(strings.join(' ') + '<br>')

// reverse the digits
// get this ['def876', 'abc000', 'zzz999', 'abc876', 'zzz888']
strings = reverse_digits_only(strings)
$('body').append(strings.join(' ') + '<br>')

// sort it
// get this ['abc000', 'abc876', 'def876', 'zzz888', 'zzz999']
strings = strings.sort()
$('body').append(strings.join(' ') + '<br>')

// reverse the digits again
// get this ['abc999' 'abc123' 'def123' 'zzz111' 'zzz000']
strings = reverse_digits_only(strings)
$('body').append(strings.join(' ') + '<br>')


function reverse_digits_only (strings) {
    return strings.map(function (string) {
        var first_three_letters = string.substring(0, 3)
        var reversed_digits = string.substring(3).split('').map(function (digit) {
            return 9 - digit
        })

        return first_three_letters + reversed_digits.join('')
    })
}

How this works is that you:

  1. Reverse the digits (abc999 becomes abc000)
  2. Then sort it naturally
  3. And then reverse the digits again.


来源:https://stackoverflow.com/questions/39444014/sorting-strings-in-java-based-on-substrings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!