0

I would like to know how to sort a String with folderIDs. The output of my String is like this:

6.8
7.4.1
10
11
1
2
3
1.1
11.1
4
1.2
10.1
2.1
5
1.3
2.2
3.1
6
2.3
3.2
4.1
7
3.3
4.2
5.1
8
4.3
5.2

I need to put these ids in my JTree and it needs to be sorted.

Then the sorted String should look like:

1
1.1
1.2
2
2.1
2.2
3
etc.

or 

1
2
3
1.1
1.2
2.1
2.2
etc.

How can I achieve this?

ᴘᴀɴᴀʏɪᴏᴛɪs
  • 7,169
  • 9
  • 50
  • 81
Boki
  • 1
  • 3
  • I assume you mean `String[]` rather than `String`. (If not, you'll have to create an array out of the single `String` object.) Did you try simply sorting the array? – Ted Hopp Dec 20 '16 at 18:47
  • 4
    Create a [custom Comparator](http://stackoverflow.com/questions/2748829/create-a-sortedmap-in-java-with-a-custom-comparator). – PM 77-1 Dec 20 '16 at 18:47
  • Ok i have now create an array out of my String like that: String[] array = new String[] {list}; How could i now sort this array ? – Boki Dec 20 '16 at 19:09
  • This array has only one element... we can't sort (reorder) one element. If your string represents many lines then you may consider splitting it on line separator. In Java `split` method takes regex and since Java 8 to represent line separator we can use `\R` so try with `String[] elements = yourString.split("\\R");`. Check if that is what you wanted, then we can think of sorting such array. – Pshemo Dec 20 '16 at 19:14

1 Answers1

0

If each input of the array is seperated by a space you can use the split() method and choose something like yourArray.split("\\s+") or if they have end line characters you could do yourArray.split("\\n") for the end of line character. This method returns your String split up based on what's in the split method and give you back an array of each piece that was split up.

For example "1.2 2.3 8.9.7 4.2".split("\\s+") gives an array that contains {"1.2", "2.3", "8.9.7", "4.2"} Once you have this array you can feed the array into the method I provided.

public static String[] sortString(String[] input)
{
    HashMap<Integer, String> mappedInputs = new HashMap<>();
    ArrayList<Integer> values = new ArrayList<>();

    for(String value : input)
    {
        //check that the input is a valid number
        if(value.replace(".", "").matches("\\d+"))
        {
            mappedInputs.put(Integer.parseInt(value.replace(".", "")), value);
            values.add(Integer.parseInt(value.replace(".", "")));
        }
    }
    //use the collections built in method to sort
    Collections.sort(values);
    String[] sortedStrings = new String[values.size()];
    //now grab the sorted numbers and add their mapped string values to the String array
    for(int i = 0; i < values.size(); i++)
    {
        sortedStrings[i] = mappedInputs.get(values.get(i));
        //debug to make sure they are sorted
        System.out.println(sortedStrings[i]);
    }

    return sortedStrings;

}

public static void main(String[] args) {
    String[] yourArray = {"1.2", "3", "2.1", "7.4.5"};
    sortString(yourArray);
}//main method

I didn't use a Comparator just to show how this could be done without one, in case you haven't learned Comparators yet, and only used basic data structures to do basically the same thing. However a Comparator is much more flexible and if you have learned how to create them, use them.

RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • Thank you for your help, but i dont get it how to call this method. I have implemented this method out of the public static void main(String[] args) {} and my array is in public static void main(String[] args) {}. So i cant feed the method with my array ... or i do it wrong. I have tried to put the method into the public static void main(String[] args) { ...} but it throw me an error. – Boki Dec 20 '16 at 19:38
  • you need to make the function static since your using it in another static method so change the function name from "public String[] sortString(String[] input)" to "public static String[] sortString(String[] input)" – RAZ_Muh_Taz Dec 20 '16 at 19:45
  • I get this message :Illegal modifier for the variable sortString; only final is permitted and this one : Void methods cannot return a value – Boki Dec 20 '16 at 19:51
  • i edited the answer to what your array should be. you shouldn't need to use a final type object – RAZ_Muh_Taz Dec 20 '16 at 20:00
  • O thank you so much your code and example works very good, but myarray doesn get sorted. Maybe the array isnt splited the right way. – Boki Dec 20 '16 at 20:14