如何对字符串编号进行排序?

时间:2019-11-19 07:18:21

标签: arrays

此字符串的输出应按以下顺序排序。 该字符串的输出应按以下顺序排序。

public static void main(String[] args) {
String input="";
 List<String> items = Arrays.asList(input.split("\\s*,\\s*"));
   System.out.println("items: " + items);
   Collections.sort(items, new Comparator<String>() {
       public int compare(String o1, String o2) {
           String o1StringPart = o1.replaceAll("\\d", "");
           String o2StringPart = o2.replaceAll("\\d", "");

           if (o1StringPart.equalsIgnoreCase(o2StringPart)) {
               return extractInt(o1) - extractInt(o2);
           }

           return o1.compareTo(o2);
       }

       int extractInt(String s) {
           String num = s.replaceAll("\\D", "");
           // return 0 if no digits found
           return num.isEmpty() ? 0 : Integer.parseInt(num);
       }
   });

   for (String s : items) {
       System.out.println(s);
   }} }

4 个答案:

答案 0 :(得分:0)

我将提供一些提示

为了比较项目1,20,200-2C,3,32C,32D,4(这是默认的排序顺序,作为字符串),您需要检查字符串中是否包含任何数字。可以使用正则表达式来完成。您可以在网上找到很多带有正则表达式的示例,这些正则表达式可以带回字符串中的数字。

修改比较器,并检查要比较的两个字符串中的任何一个是否包含任何数字,并返回适当的结果。

extractInt方法可以类似于@Pankaj Saini的建议

Integer extractInt(String s) {
    Pattern pattern = Pattern.compile("^\\d+");
    Matcher matcher = pattern.matcher(s);
    if (matcher.find()) {
        String num = matcher.group(0);
        return Integer.parseInt(num);
    }
    return null;
}

compare方法可以像这样

public int compare(String o1, String o2) {

     if(extractInt(o1)!=null && extractInt(o2)!=null){
          if(extractInt(o1).equals(extractInt(o2)))
          {
               return o1.substring(extractInt(o1).toString().length())
               .compareTo(o2.substring(extractInt(o2).toString().length()));
          }
          return extractInt(o1).compareTo(extractInt(o2));
    }
    else if(extractInt(o1)!=null)
    {
       return -1;
    }
    else if(extractInt(o2)!=null)
    {
       return 1;
    }
    else{
       return o1.compareTo(o2);
    }
}

答案 1 :(得分:0)

使用以下代码段代码。首先,需要比较字符串的整数部分,如果整数部分相等,则比较字符串部分。

import java.io.*;
import java.util.*;
import java.util.regex.*;
/*
 * To execute Java, please define "static void main" on a class
 * named Solution.
 *
 * If you need more classes, simply define them inline.
 */

class Solution {
 public static void main(String[] args) {
  String input = "605,2A,401-2A,32C,21F,201A,605A,401-1A,200-2E,583-58D,583/58E,583-57D,542,2B,1,542/2E,605B,32D,3,603,4,6,5,60,201C,542/2D,40,20,50,200-2C,21C,800A,200A,571-573B,51/2,470/1,51/1,571-573C,454-1,444-446";
  List < String > items = Arrays.asList(input.split("\\s*,\\s*"));

  System.out.println("items: " + items);

  Pattern pattern = Pattern.compile("^\\d+");

  Collections.sort(items, new Comparator < String > () {
   public int compare(String o1, String o2) {
    int intDiff = extractInt(o1) - extractInt(o2);

    if (intDiff == 0) {
     return o1.compareTo(o2);
    }

    return intDiff;
   }

   int extractInt(String s) {
    Matcher matcher = pattern.matcher(s);
    if (matcher.find()) {
     String num = matcher.group(0);
     return Integer.parseInt(num);
    }
    return 0;
   }
  });

  for (String s: items) {
   System.out.println(s);
  }
 }
}

答案 2 :(得分:0)

我假设所有数字都是整数,并且所有字母都是A到Z,您可以先将包含/-的字符串转换为浮点,然后将所有字母替换为空字符串。最后,将它们的值比较为Double
例如,51/1将是51.1,而571-573B将是571.573

代码段

public int compare(String o1, String o2) {
    String n1 = o1.replace("-", ".").replace("/", ".").replaceAll("[A-Z]", "");
    String n2 = o2.replace("-", ".").replace("/", ".").replaceAll("[A-Z]", "");

    // This equals above statements
    //String n1 = o1.replaceAll("[-/]", ".").replaceAll("[A-Z]", "");
    //String n2 = o2.replaceAll("[-/]", ".").replaceAll("[A-Z]", "");

    int result = Double.compare(Double.valueOf(n1), Double.valueOf(n2));
    return (result == 0) ? o1.compareTo(o2) : result;
}

这不是最优雅的方法,但是我认为它应该起作用!

答案 3 :(得分:0)

此答案处理20-10A和20-2A之类的数字之间的比较

public static void main(String[] args) {
        String s = "605,2A,401-2A,32C,21F,201A,605A,401-1A,200-2E,583-58D,583/58E,583-57D,542,2B,1,542/2E," +
            "605B,32D,3,603,4,6,5,60,201C,542/2D,40,20,50,200-2C,21C,800A,200A,571-573B,51/2,470/1,51/1," +
            "571-573C,454-1,444-446";

        String[] strings = s.split(",");
        Arrays.sort(strings, App::compare);
        System.out.println(Arrays.deepToString(strings));
}


public static int compare(String o1, String o2) {
    if (startsWithDelim(o1)) return compare(o1.substring(1), o2);
    if (startsWithDelim(o2)) return compare(o1, o2.substring(1));

    List<String> n1 = extractInt(o1);
    List<String> n2 = extractInt(o2);

    if (n1 != null && n2 != null) {
        Integer n1int = Integer.parseInt(n1.get(0));
        Integer n2int = Integer.parseInt(n2.get(0));
        String n1Remaining = n1.get(1);
        String n2Remaining = n2.get(1);
        int intCompare = n1int.compareTo(n2int);
        return intCompare == 0 ? compare(n1Remaining, n2Remaining) : intCompare;
    }

    if (n1 == null && n2 == null)
        return o1.compareTo(o2);
    else if (n1 == null) return -1;
    else return 1;
}

static List<String> extractInt(String s) {

    Pattern pattern = Pattern.compile("^\\d+");
    Matcher matcher = pattern.matcher(s);
    if (matcher.find()) {
        String num = matcher.group(0);
        return List.of(num, s.substring(matcher.end(0)));
    }
    return null;
}

static boolean startsWithDelim(String s) {
    return (s.startsWith("/") || s.startsWith("-"));
}
相关问题