对字符串中的字母、数字和特殊字符进行排序

huangapple 未分类评论46阅读模式
英文:

Sorting strings of letters, numbers, and special characters

问题

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Comparator;

class Main {
  public static void main(String[] args) {
    List<String> list = Arrays.asList("Bea", "hel", "Hel", "--'", "813", "!@#", "813", "813", "Alb");

    Collections.sort(list, STANDARD_ALPHABETICAL_ORDER);
    System.out.println(list);

  }

  public final static Comparator<String> STANDARD_ALPHABETICAL_ORDER =
    (a,b) -> {
        String newA = a.replaceAll("[^a-zA-Z0-9]", "");
        String newB = b.replaceAll("[^a-zA-Z0-9]", "");
        int na = newA.length();
        int nb = newB.length();
        int r;
        int n;
        if (na < nb) {
            r = -1;
            n = na;
        } else if (na > nb) {
            r = 1;
            n = nb;
        } else {
            r = 0;
            n = na;
        }
        for (int i = 0; i < n; ++i) {
            char ca = newA.charAt(i);
            char cb = newB.charAt(i);
            if (ca != cb) {
                if (Character.isDigit(ca) && !Character.isDigit(cb)) {
                  return -1;
                } else if (!Character.isDigit(ca) && Character.isDigit(cb)) {
                  return 1;
                } else if (ca > cb ) {
                  return 1;
                } else {
                  return -1;
                }
            }
        }
        return r;
    };
}
英文:

I have a list of strings that I would like to sort in a particular order.

The list is ["Bea", "hel", "Hel", "--'", "813", "!@#", "813", "813", "Alb"]

and I would like to have it sorted like:

[813, 813, 813, --', !@#, Alb, Bea, Hel, hel]

I have some code that gets pretty close but there is still one bug that I cannot figure out. Currently the program is producing this.

[813, 813, 813, Alb, --', !@#, Bea, Hel, hel]

Below is the code that I currently have

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Comparator;


class Main {
  public static void main(String[] args) {
    List&lt;String&gt; list = Arrays.asList(&quot;Bea&quot;, &quot;hel&quot;, &quot;Hel&quot;, &quot;--&#39;&quot;, &quot;813&quot;, &quot;!@#&quot;, &quot;813&quot;, &quot;813&quot;, &quot;Alb&quot;);

    Collections.sort(list, STANDARD_ALPHABETICAL_ORDER);
    System.out.println(list);

  }


  public final static Comparator&lt;String&gt; STANDARD_ALPHABETICAL_ORDER =
    (a,b) -&gt; {
        String newA = a.replaceAll(&quot;[^a-zA-Z0-9]&quot;, &quot;&quot;);
        String newB = b.replaceAll(&quot;[^a-zA-Z0-9]&quot;, &quot;&quot;);
        int na = newA.length();
        int nb = newB.length();
        int r;
        int n;
        if (na &lt; nb) {
            r = -1;
            n = na;
        } else if (na &gt; nb) {
            r = -1;
            n = nb;
        } else {
            r = 0;
            n = na;
        }
        for (int i = 0; i &lt; n; ++i) {
            char ca = newA.charAt(i);
            char cb = newB.charAt(i);
            if (ca != cb) {
                if (Character.isDigit(ca) &amp;&amp; !Character.isDigit(cb)) {
                  return -1;
                } else if (!Character.isDigit(ca) &amp;&amp; Character.isDigit(cb)) {
                  return 1;
                } else if (ca &gt; cb ) {
                  return 1;
                } else {
                  return -1;
                }
            }
        }
        return r;
    };
}

答案1

得分: 0

这部分有错误:

} else if (na &gt; nb) {
    r = -1;
    n = nb;

应该改为:

} else if (na &gt; nb) {
    r = 1;
    n = nb;

这将使得--&#39;!@#在所有其他值之前出现,因为当替换已对变量newAnewB进行时,它们将成为空字符串。如果这不是预期的结果,请进行更改。

英文:

This part is buggy:

} else if (na &gt; nb) {
    r = -1;
    n = nb;

Should be

} else if (na &gt; nb) {
    r = 1;
    n = nb;

This would make appear --&#39; and !@# before all other values, because they will be the empty string when the replacement has been done for the Variables newA or newB. You have to change this, if it is not the intended result.

huangapple
  • 本文由 发表于 2020年7月24日 06:44:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/63064179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定