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.

6
  • Do you have getters for the fields? Commented Apr 16, 2021 at 15:17
  • What is the logic? Why should a truck be sorted before a car or a bike? Commented Apr 16, 2021 at 15:18
  • Yes, I do, but I just wanted to show the structure of the class at a very basic level here. Commented Apr 16, 2021 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. Commented Apr 16, 2021 at 15:22
  • So two vehicles having the same list of groups but in different order are considered as not equal? Commented Apr 16, 2021 at 15:29

2 Answers 2

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());
});
Sign up to request clarification or add additional context in comments.

Comments

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.