How to Sort an Array Object by its attribute

Volkof

Well-Known Member
Reaction score
31
Hi all,

Lets say I have these classes:
======================================================
Code:
public class PlaneSeat {
    //Attributes
    private int seatId;
    private int customerId;
    private boolean assigned;
}

Every Plane Seat has a seatId, and only 1 customer can be assigned to it.

Assigning Seat ..
Please enter SeatID: 10
Please enter Customer ID: 10001
Seat Assigned!
======================================================

Now I have a Plane:
Code:
public class Plane {
    //Attributes
    private int totalSeats = 12;
    private PlaneSeat PlaneSeat[];
    private int numEmptySeat = totalSeats;
}
Every Plane can have up to 12 seats
======================================================

Now lets say
Code:
SeatID 1 assigned to CustomerID 1009
SeatID 2 assigned to CustomerID 1004
SeatID 3 assigned to CustomerID 1005

However I need to print out in sorted order of CustomerID, so it should become like this:
Code:
SeatID 2 assigned to CustomerID 1004
SeatID 3 assigned to CustomerID 1005
SeatID 1 assigned to CustomerID 1009


How do I sort the PlaneSeat objects in ascending order of their CustomerID?
To my knowledge, I should use:
Code:
import java.util.Arrays;
 
Arrays.sort()

Something like
Arrays.sort(PlaneSeats, Sort by CustomerID)?


Thanks in advance
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
You should read the JDoc for Comparators:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Comparator.html
Then you should be able to write your own comparator, a subclass of the java comparator, which will work with PlaneSeats and sort them.
Then you can call
java.util.Collections.sort(List, Comparator)
java.util.Arrays.sort(Object[], Comparator)
to sort your list or array.

Basically all you need to implement is
compare(Object o1, Object o2)
Which returns an int which is
positive if o1 > o2,
zero if 01 == o2,
and negative if o1 < o2

furthermore sgn(compare(o1, o2) == -sgn(compare(o2, o1)) should be true.
 

DrEvil

FCRI Associate!
Reaction score
111
Just curious, but do you really need the 'private boolean assigned;'
If no customer is assigned to a PlaneSeat the customerID should be some sort of default value (0).
Meaning you could just check whether customerID!=0 (means its assigned...)
 

Volkof

Well-Known Member
Reaction score
31
@Accname

Yes I did come across Comparator, I know I need it, but dont understand how to use it.

So I'm suppose to create a separate Comparator Class, and define the Sort Algorithm there? But that is the confusing part. How do I even code the Comparator Class?

@DrEvil
I cant use CustomerID. What if it is also set as 0?
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Yes, the comparator is a seperate class whose only purpose is to compare two objects of a certain kind.

Here's an example for the comparator class:

Code:
public class SortByEmptySeats implements Comparator{
 
    public int compare(Object o1, Object o2) {
        Plane p1 = (Plane ) o1;
        Plane p2 = (Plane ) o2;
        return p1.getEmptySeats() - p2.getEmptySeats();
    }
}

Then you can use the sort() method to sort your container according to your custom comparator.

Code:
Collections.sort(  yourListOfPlanes, new SortByEmptySeats()  );
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top