java如何更改排序中的优先级

mfuanj7w  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(365)

我知道ascii排序在java中很容易使用collections.sort等内置方法,使用coparator和comparable接口,但我想知道有没有按标准字母顺序排序的简单方法
使用ascii排序的输出为:“2012010”、“2012011”、“2012012”、“201201a”
使用标准字母顺序的输出将是:“201201a”“2012010”、“2012011”、“2012012”,
我想知道java中是否有其他方法可以使用可比较或比较接口来获得这种标准的字母排序
下面的程序提供ascii排序,但我想要标准的字母顺序

  1. public class AlphabeticalSort {
  2. public static void main(String args[]) throws NoSuchFieldException, SecurityException{
  3. String[] words = { "2012010", "2012012", "2012011", "201201A" };
  4. for (int i = 0; i < 4; ++i) {
  5. for (int j = i + 1; j < 4; ++j) {
  6. if (words[i].compareTo(words[j]) > 0) {
  7. String temp = words[i];
  8. words[i] = words[j];
  9. words[j] = temp;
  10. }
  11. }
  12. }
  13. System.out.println("In lexicographical order:");
  14. for (int i = 0; i < 4; i++) {
  15. System.out.println(words[i]);
  16. }
  17. }
  18. }
nfzehxib

nfzehxib1#

你可以用这样的比较器:

  1. public final static Comparator<String> STANDARD_ALPHABETICAL_ORDER =
  2. (a,b) -> {
  3. int na = a.length();
  4. int nb = b.length();
  5. int r;
  6. int n;
  7. if (na < nb) {
  8. r = -1;
  9. n = na;
  10. } else if (na > nb) {
  11. r = -1;
  12. n = nb;
  13. } else {
  14. r = 0;
  15. n = na;
  16. }
  17. for (int i = 0; i < n; ++i) {
  18. char ca = a.charAt(i);
  19. char cb = b.charAt(i);
  20. if (ca != cb) {
  21. if (Character.isDigit(ca) && !Character.isDigit(cb)) {
  22. return 1;
  23. } else if (!Character.isDigit(ca) && Character.isDigit(cb)) {
  24. return -1;
  25. } else if (ca < cb) {
  26. return -1;
  27. } else {
  28. return 1;
  29. }
  30. }
  31. }
  32. return r;
  33. };

然后使用它对数组进行排序:

  1. String[] words = { "2012010", "2012012", "2012011", "201201A" };
  2. Arrays.sort(words, STANDARD_ALPHABETICAL_ORDER);
  3. System.out.println("In lexicographical order:");
  4. for (int i = 0; i < 4; i++) {
  5. System.out.println(words[i]);
  6. }
展开查看全部
wfsdck30

wfsdck302#

你可以这样编码
Package 试验;
导入java.util.arrays;导入java.util.comparator;
公共类字母排序{

  1. public static void main(String args[]) throws NoSuchFieldException,
  2. SecurityException {
  3. String[] words = { "2012010", "2012012", "2012011", "2012011A","2012011B" };
  4. Arrays.sort(words, new Comparator<String>() {
  5. @Override
  6. public int compare(String o1, String o2) {
  7. int result=0;
  8. if (isNumber(o1) && isNumber(o2)) {
  9. result= Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
  10. } else if (!isNumber(o1) && isNumber(o2)) {
  11. Integer o1num = Integer.valueOf(o1.replaceAll("[^-?0-9]+",
  12. ""));
  13. if (o1num == Integer.valueOf(o2)) {
  14. result= -1;
  15. } else {
  16. result= o1num.compareTo(Integer.valueOf(o2));
  17. }
  18. } else if (!isNumber(o2) && isNumber(o1)) {
  19. Integer o1num = Integer.valueOf(o2.replaceAll("[^-?0-9]+",
  20. ""));
  21. if (o1num == Integer.valueOf(o1)) {
  22. result= +1;
  23. } else {
  24. result= o1num.compareTo(Integer.valueOf(o1));
  25. }
  26. } else {
  27. result= o1.compareTo(o2);
  28. }
  29. System.out.println("o1="+o1+" o2="+o2+" result= "+result);
  30. return result;
  31. }
  32. });
  33. System.out.println("In lexicographical order:");
  34. for (int i = 0; i < words.length; i++) {
  35. System.out.println(words[i]);
  36. }
  37. }
  38. public static boolean isNumber(String val) {
  39. boolean isNumber = false;
  40. try {
  41. Integer.parseInt(val);
  42. isNumber = true;
  43. } catch (NumberFormatException e) {
  44. // e.printStackTrace();
  45. }
  46. return isNumber;
  47. }

}

展开查看全部

相关问题