Sorting a string with possible numbers in it

I have the following possible addresses as a string (not sorted):

"road 21"
"road 1"
"road 186"
"road +21 / 23"
"road +21 / 19"
"another road 21"
"another road 1"

and I want to be able to sort them as (and not by default default sort order):

another road 1
another road 21
road 1
road 21
road +21 / 19
road +21 / 23
road 186

How should I do it? I should probably use a custom comparator, but how do I split the string?

+4
source share
5 answers

I implemented this in Java, and I know that at first it looks weird.

If you have any questions, feel free to ask me

public class SpecialComparator implements Comparator<String> {

   @Override
    public int compare(String arg0, String arg1) {
     // TODO Auto-generated method stub
     String []words1=arg0.split(" ");
     String [] words2 = arg1.split(" ");
     int i = 0;

        if (words1[i].hashCode()>words2[i].hashCode()){
            return 1;
        }
        else if (words1[i].hashCode()<words2[i].hashCode()){
            return -1;
        }
        else if (words1[i].hashCode()==words2[i].hashCode())
            return compare(arg0.substring(i+1, arg0.length()), arg1.substring(i+1,arg1.length()));
        else if (i == Math.min(words1.length,words2.length)-1 && Math.min(words1.length,words2.length) == words1.length){
            return -1;
        }
        else if (i == Math.min(words1.length,words2.length)-1 && Math.min(words1.length,words2.length) == words2.length){
            return 1;
        }
        else if (i == Math.min(words1.length,words2.length)-1 && words1.length == words2.length){
            return 0;
        }
        else{
            return 0;
        }


   }


   public static void main (String[] args){
    ArrayList<String> input = new ArrayList<String>();
    SpecialComparator a = new SpecialComparator();
    input.add("road 21");
    input.add("road 1");
    input.add("road 186");
    input.add("road +21 / 23");
    input.add("road +21 / 19");
    input.add("another road 21");
    input.add("another road 1");
    Collections.sort(input,a);
    for (String ans : input){
        System.out.println(ans);
    }

  }



 }
+3
source

Your format is as follows:

  • {name}
  • {number}
  • optional slash character
  • Additional second {number}.

, , , :

public class MyInput {
  private String name;
  private Integer firstNumber;
  private Integer secondNumber;
}

, List<MyInput>.

, Comparator Collections.sort(yourList, yourCustomComparator)

+1
0

,

List<String> list = Arrays.asList("road 21", "road 1", "road 186",
                "road +21 / 23", "road +21 / 19", "another road 21",
                "another road 1");

        Collections.sort(list, new Comparator<String>() {

            Integer toNumber(String string) {
                try {
                    return Integer.parseInt(string);
                } catch (NumberFormatException e) {
                    return null;
                }
            }

            @Override
            public int compare(String o1, String o2) {
                String[] left = o1.split("\\s+");
                String[] right = o2.split("\\s+");
                for (int i = 0; i < Math.min(left.length, right.length); i++) {
                    String ls = left[i];
                    String rs = right[i];

                    Integer li = toNumber(ls);
                    Integer ri = toNumber(rs);

                    if (li != null && ri != null
                            && li.intValue() != ri.intValue()) {
                        return li.intValue() - ri.intValue();
                    } else if (li != null && ri == null) {
                        return 1;
                    } else if (li == null && ri != null) {
                        return -1;
                    } else if (li == null && ri == null){
                        int compared = ls.compareToIgnoreCase(rs);
                        if (compared != 0) {
                            return compared;
                        }
                    }

                }
                return left.length - right.length;
            }
        });

, ,

0

:

class MyComparator implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        o1 = o1.replace("+", "");
        o2 = o2.replace("+", "");

        String[] a1 = o1.split(" ");
        String[] a2 = o2.split(" ");
        int length = (a1.length > a2.length) ? a2.length : a1.length;

        for (int i = 0; i < length; i++) {
            if (!a1[i].equalsIgnoreCase(a2[i])) {
                if (!isIntegerRegex(a1[i]) || !isIntegerRegex(a2[i])) {
                    return o1.compareTo(o2);
                }
                int f = Integer.parseInt(a1[i]);
                int s = Integer.parseInt(a2[i]);
                return f - s;
            }
        }

        return a1.length - a2.length;
    }

    public boolean isIntegerRegex(String str) {
        return str.matches("^[0-9]+$");
    }
}

:

public String[] sortStrings(String[] input) {
    Arrays.sort(input, new MyComparator());
    return input;
}
0

Source: https://habr.com/ru/post/1547741/


All Articles