Java实现字符串排序的几种方式

Love The Way You Lie 2023-09-27 20:24 183阅读 0赞

创建实体类(此处引入了lombok)

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class Test{
  5. private int Id;
  6. private String TestNo;
  7. }

一、使用List集合中自带的sort方法(字符串的位数保持一致,不一致的情况可以在左边补0,也可以使用String.format()方法补全)

1、在对象排序中使用

  1. public static void main(String[] args) {
  2. List<Test> testList= new ArrayList<>();
  3. testList.add(1,"22");
  4. testList.add(2,"11");
  5. testList.add(3,"44");
  6. testList.add(4,"33");
  7. list.sort((a,b)->a.getTestNo().compareTo(b.getTestNo()));
  8. }

2、在字符串排序中使用

  1. public static void main(String[] args) {
  2. List<String> testList= new ArrayList<>();
  3. testList.add("22");
  4. testList.add("11");
  5. testList.add("44");
  6. testList.add("33");
  7. list.sort(String::compareTo);
  8. }

二、使用Stream流(字符串的位数保持一致,不一致的情况可以在左边补0,也可以使用String.format()方法补全)

1、在对象排序中使用

  1. public static void main(String[] args) {
  2. List<Test> testList= new ArrayList<>();
  3. testList.add(1,"22");
  4. testList.add(2,"11");
  5. testList.add(3,"44");
  6. testList.add(4,"33");
  7. List<Test> sortList = testList.stream().sorted(Comparator.comparing(Test::getTestNo).collect(Collectors.toList());
  8. }

2、在字符串排序中使用

  1. public static void main(String[] args) {
  2. List<String> testList= new ArrayList<>();
  3. testList.add("22");
  4. testList.add("11");
  5. testList.add("44");
  6. testList.add("33");
  7. List<String> collect = testList.stream().sorted(Comparator.comparing(Objects::toString)).collect(Collectors.toList());
  8. }

三、使用基数排序(此处仅展示对字符串进行排序,不需要补全位数)

  1. class Quick3string{
  2. //三向字符串快速排序
  3. private static int charAt(String s, int d) {
  4. if(d < s.length()) {
  5. return s.charAt(d);
  6. }
  7. return -1;
  8. }
  9. public static void sort(String[] a) {
  10. sort(a, 0, a.length - 1, 0);
  11. }
  12. private static void sort(String[] a, int lo, int hi, int d) {
  13. if(hi <= lo) {
  14. return;
  15. }
  16. int lt = lo, gt = hi, i = lo + 1;
  17. int v = charAt(a[lo], d);
  18. while(i <= gt) {
  19. int t = charAt(a[i], d);
  20. if(t < v) {
  21. exch(a, lt++, i++);
  22. }else if(t > v) {
  23. exch(a, i, gt--);
  24. }else {
  25. i++;
  26. }
  27. }
  28. //a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi]
  29. sort(a, lo, lt - 1, d);
  30. if(v >= 0) {
  31. sort(a, lt, gt, d + 1);
  32. }
  33. sort(a, gt + 1, hi, d);
  34. }
  35. private static void exch(String[] a, int i, int j) {
  36. String t = new String(a[i]);
  37. a[i] = a[j];
  38. a[j] = t;
  39. }
  40. public static void main(String[] args) {
  41. String[] a = {"48328458C70490693231303331361020", "48326E48E1136A9E3139313131301020", "48326E48E1176F8A3139313131311020", "48326E48E12474713139313131311020"};
  42. Quick3string.sort(a);
  43. System.out.println(Arrays.toString(a));
  44. }
  45. }

发表评论

表情:
评论列表 (有 0 条评论,183人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java实现异步方式

    Java实现异步的几种方式 > 异步编程在对响应时间近乎严苛的今天,受到了越来越多的关注,尤其是在IO密集型业务中。对比传统的同步模式,异步编程可以提高服务器的响应时间和

    相关 字符串拼接方式

    \+ 号拼接 通过`+`拼接是最常见的拼接方式,这个应该算是最简单的一种方式了,但是很遗憾得玩告诉你,阿里巴巴在他们的规范里面之处不建议在 for 循环里面使用 “+”

    相关 java实现同步方式

    为何要同步? > java允许许多线程并发控制,当多个线程同时操作一个可共享的资源变量是(如数据的增、删、改、查),将会导致数据不准确,相互之间产生冲突,因此加入同步锁以避免