0

I have an array holding 8 objects similar to this..

`public class event
{
    public int guests;
    public double price;
    public String date;
    public event(int x, int y, String z)
    {
        guests = x;
        price = y;
        date = z;
    }
 }`

The entire array of objects must be sorted based on the comparison of each arrays data for a specific field. So I would need a class each for guests, price, and the title?

I have found examples of doing something like this for two objects by implementing comparator but I do not understand how to use this for more than two objects...

Thanks.

Grim
  • 1
  • 2
    Do you mean that you would like 8 different sorting criteria for this class `event`? Or do you mean that you would like 1 sorting criteria for this class that utilizes each of properties of the `event` for the comparison? – NoseKnowsAll Apr 17 '15 at 16:26
  • In the first case, you should create 8 different methods: `compareGuestsTo(event other)`, `comparePriceTo(event other)` etc. In the second case, make sure this class implements `Comparable` and has a `compareTo(event other)` that is set up as you like it. – NoseKnowsAll Apr 17 '15 at 16:28
  • If you have an array, use `Arrays#sort` rather than `Collections#sort`, but the concept remains the same. – Luiggi Mendoza Apr 17 '15 at 16:36
  • I don't know why you think the fact there are 8 objects is a problem. The `compare` method of a `Comparator` uses 2 objects, but when you sort an array, the `compare` method is called for lots of pairs. – Paul Boddington Apr 17 '15 at 16:38

1 Answers1

0

Use static sort method of Arrays class like:

Arrays.sort(myarray, new Comparator<Events>());
SMA
  • 36,381
  • 8
  • 49
  • 73