I need to know if anyone can help me in the right direction when trying to numericly sort the following data.
The data is a string which contains up to five levels of integer type data all separated by dots, eg 184.108.40.206.4.
Do your objects have the same level of nesting for all? Do they all contain 4 dots? You can create a Comparator<IntQuintuplet> quite simply. Or you can say that class supports natural order, in which case it may be simpler for it to implement Comparable<IntQuintuplet>. Start by reading about object ordering in the Java™ Tutorials. You can also use a Stream for sorting:-The meaning of lines 1-4 should be obvious.
You can get a Stream<T> from any List, and the Stream is implicitly the same generic type as the List, so don't try it on raw types.
You can pass a Comparator<T> to the sorted method as a parameter, or you can write a λ. If the type of the Stream implements Comparable (or more precisely T extends Comparable<? super T>), then you can use the no‑arguments version of Stream#sorted(). Using sorted() may require much memory until the sorting process is completed. That will probably only be a problem if you have many millions of items in the Stream.
The meaning of the toList method should be obvious. You get a List<T> implementation, but the Stream documentation doesn't specify what sort of List. If you want a particular implementation you can try this instead of line 8:-
.collect(Collectors.toCollection(ArrayList::new)); The toCollection method returns a Collection and the ::new bit means you are calling a constructor of the type before :: so you get an ArrayList.
There is a simpler method if you don't mind losing the original order:-
Assuming you have 4 dots in every item, then a typical IntQuintuplet might have this structure
Stephan is right.
Also don't use underscores in variable names.
You should split the String with "\\." You have to escape the dot with \\ because it is a metacharacter. At least I think that is correct. Don't hardcode 10. Don't catch the Exception, except possibly to throw a different exception. If you have an array with other than 5 elements, or a non‑integer, let the Exception go and crash the program.
What do the four fields mean? I thought you would only need 1 field.
So here's a complete class that encapsulates all the logic for converting between int arrays and leveled strings, and imposing a natural ordering on them. I think an important thing to note is that it concerns itself with the levels only, and not other data that happens to be specific to the application. That means that it can be reused for various purposes (paragraph numbering, version numbering, etc.?). Another very important aspect is that it's immutable. That makes it perfect to use as a key in maps, among other things. Take a close look, and if you have questions for us, please don't hesitate.
As I am stuck with Java 7 I was forced to change the constructor implementations a bit and managed to get it working. I changed my own implementation based of yours and sorted out a couple of minor kinks in my own code.
I hope you draw inspiration from it for your future classes. Keep them simple. Make them immutable when possible and convenient. Always consider if it makes sense to make them Comparable. Override equals() and hashCode() when it makes sense. Check your parameters and throw exceptions when they're invalid, or when your object is in a wrong state.
You should also be precise in what behavior you expect from your classes, and document them as such. For example, look how the IntHierarchy makes a clear distinction between when it returns/expects non-negative integers and positive integers. Don't make users of your class guess how it behaves.
They are essentially the same. Dot is just a character class that matches every character (except for newlines, unless the DOTALL flag is used). "\\" escapes it, and "" treats it as a simple character. Likewise, "$" means 'end of input', and "\\$" and "[$]" both mean 'dollar sign'.