-5

Hey everybody I have object which stores multiple integer values. I want it to sort it multiple times by descending order, one time descending by "Wins" value, another time descending by "Played" value etc... How can I do that ?

Ján Kluka
  • 11
  • 2
  • First of all what do you mean with "I hava object which stores..."? Furthermore you should provide the version of Java you use and provide some code snippets. – rufer7 Jan 05 '18 at 12:33
  • If the duplicate doesn't help, please check out these [similar questions](https://www.google.com/search?q=site%3Ahttps%3A%2F%2Fstackoverflow.com%2F+java+sort+in+different+ways), and in the future, please search first, and show the fruits of your efforts with your question, including your [mcve] code attempt. This will lead to better questions for us and better help from you. – Hovercraft Full Of Eels Jan 05 '18 at 12:36
  • In case you want to have a combined order (i.e. order by wins, and order equal wins by played), I find [this answer](https://stackoverflow.com/a/39571813/7653073) more suitable. – Malte Hartwig Jan 05 '18 at 12:37

1 Answers1

0

Here is the sample code for the problem

    public class MyObject  {
        private int wins;
        private int played;         
        // to do add getter and setter method for the above fields
    }

    List<MyObject> objects = new ArrayList<MyObject>();
    // to do java code for creating Myobjects

Before Java 8 :

// wins sorting
objects.sort(new Comparator<MyObject>() {
    @Override
    public int compare(MyObject m1, MyObject m2) {
        return m2.getWins() - m1.getWins();
     }
});

// Played sorting
objects.sort(new Comparator<MyObject>() {
    @Override
    public int compare(MyObject m1, MyObject m2) {
        return m2.getPlayed() - m1.getPlayed();
     }
});

Java 8 Code:

objects.sort(Comparator.comparingInt(MyObject::wins) .reversed());
objects.sort(Comparator.comparingInt(MyObject::played) .reversed());