sorting - Java - Comparing unrelated objects with a custom order -


I want to sort a list containing objects that are unrelated by providing a custom order. For example, the list that I want to sort in, which includes classes animals. I type an object in a animal object that can be a cat, mouse or dog. I have a custom order mouse & lt; Cat & lt; dog. How can I find that the list of animals is sorted by my custom order?
In particular, the following codes are given, I do not know how to implement a comparator, apart from this it is a simplified example, I can not modify the dog, cat, mouse nor the animals in classes.

  // Cat and mouse similar public square dog {...} public class animal {object type; Public animal (object aType) {type = aType; } // matching, setter etc.} comparing public class {list & gt; Animals & gt; Animals; Public comparison (dog) = dog (new dog); Animal animal 1 = new animal (dog); Animals.add (animal1); Cat cat1 = new cat (); Animal animal 2 = new animal (cat); Animals.add (animal2); // Type dog, cat or other elements of the mouse. // Sort the list with custom order: mouse & lt; Cat & lt; Dog collection.Source (animal, int comparter); } Personal Comparison & lt; Animals & gt; Int Comparator () {New Comparative & lt; Animals & gt; () {@ Compared to Override Public Int (animal first, animal second) {Return ???; }}; }}  

A solution will be to manually check the sequence in the type of class (through the example) and the hard code comparator. Although I have a lot of classes that can be included in the animal (not just three as the example), so if this matter is important.

  Personal comparison & lt; Animals & gt; Int Comparator () {New Comparative & lt; FXNode & gt; () {@ Compared to override public int (animal first, animal second) {// if the first mouse, then return -1 because it is the 'smallest' object (if first type .get () for example mouse) Return -1; // if there is a cat first and second mouse, return 1 etc., there are many cases}  

I hope your example is bad and you do not really do this because it is actually a poor concept, maybe you should reconsider your class hierarchy because I do not get the issue of sorting unrelated objects.

Since you have omitted a code, your code has a comparative & lt; FXNode & gt; In order to process some method in a predefined order, you want to sort the graphic components. If I think what you want to do, then this order is based on the runtime type of FXNode. If this is the case, then you can do this:

  The public class applies custom compiler to Comparator & lt; FXNode & gt; {Static map & lt; Classroom & lt;? FXNode & gt; Integer & gt; Preferences = New Hashmap & lt; & Gt; (); Fixed {preferences} (Node 1. class, 2); Priorities.put (Node2.class, 1); } Compare @ Override Public Int (FxNode O1, FxNode O2) {Return Integral Copier (Priorities. Oat (o 1.GetClass ()), Priorities. (O 1.getClass ())); However, in more general terms, I would advise you to compare only those items which are logically identical and as a result, share certain characteristics or methods. 


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -