1

I am having a scenario where I have a list of objects say List<Vehicle>. Each of the Vehicle object has a list of another object say Group. So the Vehicle object has a property called groups which is a List<Groups>.

Basically, the structure is like this :

public class Vehicle {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer wuid;
    
    private String description;

    @ManyToMany
    @JoinTable(
        name="wuclassification"
        , joinColumns={
            @JoinColumn(name="wuid")
            }
        , inverseJoinColumns={
            @JoinColumn(name="classid")
            }
        )
    private List<Group> groups;
}

And the Group class is like :

public class Group {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer groupId;

    private String name;
}

So now I have a list of Vehicle objects with its list of Group objects. Is there a way of sorting the work unit objects based on the list of group objects with their name attribute?

For example, say I have a list of Vehicle objects which has a Bike, Car and Truck. And then the list of groups with their names for this vehicles are :

Bike : ['short distance','bike taxi']
Car : ['goods career','long distance','short distance','taxi']
Truck : ['goods carrier','long distance']

So sorting this based on the list of groups should look like :

Truck : ['goods carrier','long distance']
Car : ['goods career','long distance','short distance','taxi']
Bike : ['short distance','bike taxi']

I followed this link of sorting List<List> from this link : Java Sort List of Lists

But I am not able apply a similar approach in this scenario. How can I can implement the sorting? Would gladly appreciate your help and thanks in advance.

Aravind S
  • 463
  • 2
  • 9
  • 25
  • Do you have getters for the fields? – Unmitigated Apr 16 '21 at 15:17
  • What is the logic? Why should a truck be sorted before a car or a bike? – Eritrean Apr 16 '21 at 15:18
  • Yes, I do, but I just wanted to show the structure of the class at a very basic level here. – Aravind S Apr 16 '21 at 15:19
  • @Eritrean the requirement is such that we want to sort the results in alphabetical order of the group names for a list of vehicles. – Aravind S Apr 16 '21 at 15:22
  • So two vehicles having the same list of groups but in different order are considered as not equal? – Eritrean Apr 16 '21 at 15:29
  • @Eritrean to explain the scenario I had included only used a few groups to avoid complications. But in the actual scenario, the groups are different based on the vehicle type and this won't lead to having the same groups for different vehicles. – Aravind S Apr 16 '21 at 15:33

2 Answers2

4

You can create a custom Comparator to compare elements at corresponding indexes in the Lists.

vehicles.sort((a,b)->{
   List<Group> aGroups = a.getGroups(), bGroups = b.getGroups();
   for(int i = 0; i < Math.min(aGroups.size(), bGroups.size()); i++){
      int cmp = aGroups.get(i).getName().compareTo(bGroups.get(i).getName());
      if(cmp != 0) return cmp;
   }
   return Integer.compare(aGroups.size(), bGroups.size());
});
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

To add another alternative: I would create a function which takes a list of groups and returns a string by joininig the names of the groups, then i would create a comparator which compares the strings returned by the mentioned function. Something like:

Function<List<Group>,String> extractGroupNames = list -> list.stream()
                                                             .map(Group::getName)
                                                             .collect(Collectors.joining());

Comparator<Vehicle> byGroupNames = (v1,v2) -> extractGroupNames.apply(v1.getGroups())
                                                  .compareTo(extractGroupNames.apply(v2.getGroups()));

Then sort like:

vehicles.sort(byGroupNames);

or if you need the reversed order

 vehicles.sort(byGroupNames.reversed());
Eritrean
  • 15,851
  • 3
  • 22
  • 28