0

I want to sort a string Array in which all cells begin with 1 or 2 digits, the rest contains text.
However, just a Arrays.sort(myArray) is sorting only compared to the first digit, so I get this:

1  - XXX
10 - XXX
12 - XXX
2  - XXX
24 - XXX

Does anyone have the solution?

3
  • What is your input, what is your output and what is the output you want! Commented Sep 21, 2018 at 14:19
  • 5
    Use a custom Comparator. Plenty of literature around. Commented Sep 21, 2018 at 14:19
  • Maybe include the sample data as well in your question. Also the code to create the sample data. Otherwise everybody needs to copy paste your table. That slows down the answering speed Commented Sep 21, 2018 at 14:20

4 Answers 4

3

You need to provide your own Comparator for this. Here is an example:

Arrays.sort(yourArray, new Comparator<String>() {
    @Override
    public int compare(String string1, String string2) {
        int number1 = Integer.decode(string1.substring(0, string1.indexOf(" "))); // This assumes that there is always a space after the number
        int number2 = Integer.decode(string2.substring(0, string2.indexOf(" ")));
        return Integer.compare(number1, number2);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Compare the rest of the text in case number1 == number2.
That's up to OP and how he wants it to behave
1

Mischa has provided the correct answer but, just as a bit of fun, this works too:

Arrays.sort(arr, (s1, s2) -> 
{
  return (s1.charAt(1)-s2.charAt(1))*10 + s1.charAt(0)-s2.charAt(0);
});

Comments

0

public class SortArray {

public static void main(String[] args) {

    List<Digit> listDigit = new ArrayList<Digit>();

    Digit digit1 = new Digit(1, "XXX");
    Digit digit2 = new Digit(10, "XXX");
    Digit digit3 = new Digit(12, "XXX");
    Digit digit4 = new Digit(2, "XXX");
    Digit digit5 = new Digit(24, "XXX");

    listDigit.add(digit2);
    listDigit.add(digit3);
    listDigit.add(digit1);
    listDigit.add(digit4);
    listDigit.add(digit5);

    for (Digit digit : listDigit) {
            System.out.println("Sort Before -  Digit: "+digit.getDigit() +" Name:"+digit.getName());
    }

    System.out.println(" -------------------//-------------------------");

     Collections.sort(listDigit, new SortbyDigit()); 

    for (Digit digit : listDigit) {
            System.out.println("Sort After -  Digit: "+digit.getDigit() +" Name:"+digit.getName());
    }

}

public class Digit {

private final int digit;
private final String name;

public Digit(int digit, String name) {
    this.digit = digit;
    this.name = name;
}

public int getDigit() {
    return digit;
}

public String getName() {
    return name;
}

@Override
public int hashCode() {
    int hash = 7;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Digit other = (Digit) obj;
    if (this.digit != other.digit) {
        return false;
    }
    if (!Objects.equals(this.name, other.name)) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Digit{" + "digit=" + digit + ", name=" + name + '}';
}

}

public class SortbyDigit implements Comparator {

 @Override
public int compare(Digit a, Digit b) 
{ 
    return a.getDigit() - b.getDigit(); 
} 

}

Sort Before - Digit: 10 Name:XXX Sort Before - Digit: 12 Name:XXX Sort Before - Digit: 1 Name:XXX Sort Before - Digit: 2 Name:XXX Sort Before - Digit: 24 Name:XXX -------------------//------------------------- Sort After - Digit: 1 Name:XXX Sort After - Digit: 2 Name:XXX Sort After - Digit: 10 Name:XXX Sort After - Digit: 12 Name:XXX Sort After - Digit: 24 Name:XXX

Comments

-1

You can use a regex to find the number at the start of the string in a custom compactor for sorting. I chose to use a regex so the length of the number does not matter, and neither does what comes after the number. If both strings start with a number and the numbers are not the same1, compare the numbers, if not just compare the two strings as a whole.

Arrays.sort(array, (s1, s2) -> {
    Pattern pattern = Pattern.compile("^(\\d+)");

    Matcher matcher1 = pattern.matcher(s1);
    Matcher matcher2 = pattern.matcher(s2);

    if (matcher1.find() && matcher2.find()) {
        Integer i1 = Integer.valueOf(matcher1.group(1));
        Integer i2 = Integer.valueOf(matcher2.group(1));

        int compared = i1.compareTo(i2);
        if (compared != 0) {
            return compared;
        }
    }

    return s1.compareTo(s2);
});

For your example array this would output:

1  - XXX
2  - XXX
10 - XXX
12 - XXX
24 - XXX

1: If the numbers are the same, you can still compare the whole string so that everything after the number will still be sorted.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.