How to sort this list of strings along with the strings and output the result as expected?

88 Views Asked by At

When the input is:

["xL01(F]J","2pn5Mm","-5)8gF{","KWq0P]*%Q","n@,:\u003eAm@","\u003cRN_qCa7","8Qx\u0026RAON","gT~s!1s?4i{K","w\"r^d_#l$Mmp"]

Expected output should be:

["(01FJL]x","25Mmnp",")-58Fg{","%*0KPQW]q",",:\u003e@@Amn","7\u003cCNR_aq","\u00268ANOQRx","!14?KTgiss{~","\"#$M^_dlmprw"]

There are specific Unicode characters (\u003e, \u003c, \u0026) that were being sorted and replaced in a specific way. How to create a solution that could handle these Unicode characters dynamically without explicitly checking for each one individually. By identifying the Unicode characters programmatically, the solution should adapt to any input string containing such characters without requiring manual intervention.

public static void main(String[] args) {
    String[] qArray = { "xL01(F]J", "2pn5Mm", "-5)8gF{", "KWq0P]*%Q", "n@,:\u003eAm@", "\u003cRN_qCa7",
            "8Qx\u0026RAON", "gT~s!1s?4i{K", "w\"r^d_#l$Mmp" };
    List<String> aList = new ArrayList<>();
    List<String> qList = Arrays.asList(qArray);
    qList.forEach(qString -> {
        String sorted = sortedString(qString);
        String replacedAndSorted = replaceUnicode(sorted);
        aList.add(replacedAndSorted);
    });
    System.out.println(Arrays.toString(aList.toArray()));
}

public static String sortedString(String str) {
    char[] c = str.toCharArray();
    Arrays.sort(c);
    return new String(c);
}

public static String replaceUnicode(String sorted) {
    String replacedString = sorted;
    char[] c = sorted.toCharArray();
    if (sorted.contains("\u003e")) {
        replacedString = sorted.replace("\u003e",
                "\\u0" + Integer.toHexString(sorted.codePointAt(sorted.indexOf("\u003e"))));
    }
    if (sorted.contains("\u003c")) {
        replacedString = sorted.replace("\u003c",
                "\\u0" + Integer.toHexString(sorted.codePointAt(sorted.indexOf("\u003c"))));
    }
    if (sorted.contains("\u0026")) {
        replacedString = sorted.replace("\u0026",
                "\\u0" + Integer.toHexString(sorted.codePointAt(sorted.indexOf("\u0026"))));
    }
    return replacedString;
}
1

There are 1 best solutions below

7
gmifflen On

(removed my previous 2 answers)
After thinking on it for a bit, this is the best solution I can think of which give the exact output you mention in the main post:
Output: ["(01FJL]x","25Mmnp",")-58Fg{","%*0KPQW]q",",:\u003e@@Amn","7\u003cCNR_aq","\u00268ANOQRx","!14?KTgiss{~",""#$M^_dlmprw"]

public static void main(String[] args) {
  List<String> input =
      Arrays.asList(
          "xL01(F]J",
          "2pn5Mm",
          "-5)8gF{",
          "KWq0P]*%Q",
          "n@,:\u003eAm@",
          "\u003cRN_qCa7",
          "8Qx\u0026RAON",
          "gT~s!1s?4i{K",
          "w\"r^d_#l$Mmp"); // the `"` needs to be escaped,
                            // it isn't in your input, 
                            // but it should be to work in a string

  List<String> sortedStrings = input.stream()
                                    .map(Main::sortString)
                                    .collect(Collectors.toList());

  System.out.println("Input: " + formatList(input));
  System.out.println("Output: " + formatList(sortedStrings));
}

private static String sortString(String str) {
  char[] chars = str.toCharArray();
  Arrays.sort(chars);
  return new String(chars);
}

private static String formatList(List<String> list) {
  return list.stream()
             .map(Main::escapeUnicodeCharacters)
             .map(str -> "\"" + str.replace("\"\"", "\"\\\"\"") + "\"")
             .collect(Collectors.joining(",", "[", "]"));
}

private static String escapeUnicodeCharacters(String str) {
  return str.replace(">", "\\u003e")
            .replace("<", "\\u003c")
            .replace("&", "\\u0026");
}