-1

There exists some questions regarding sorting by string variables of custom objects. I did follow them and used comparators. However, in my custom object there are multiple string and Integer values and I want to sort objects with different attribute unpon whatever was requested. I followed this to just sort one by one attribute.

For example

public class CustomerObj(){
    Integer id;
    String name;
    String city;
    Integer zipCode;
}

I will only need to sort by ONE of the attributes each time the user requests. On top of this user will also request whether they want the list to be sorted in ascending or desending order. So for example if I were to get a request from use to sort it by Id and in desceding order I am unable to pass in a comparator and Collections.reverseOrder();

Summary: I want to sort by different attributes (not multiple at the same time) with whatever attribute requested and with whichever order requested. Example User request sort by id in descending order. response:

[
   obj{id: 3},
   obj{id: 2},
   obj{id: 1}
]
  • create different methods for each attribute, check the request and call the method according to the attribute. – saravanan Nov 07 '21 at 00:42

1 Answers1

1

A map/list/enum of comparators may be implemented and then a single comparator may be selected for example using a String name:

static Map<String, Comparator<CustomObject>> comparators = Map.of(
    "id", Comparator.comparingInt(CustomObject::getId),
    "id desc", Comparator.<CustomObject>comparingInt(CustomObject::getId).reversed(),
    "name", Comparator.comparing(CustomObject::getName),
    "name desc", Comparator.comparing(CustomObject::getName, Comparator.reverseOrder()),
    "city", Comparator.comparing(CustomObject::getCity),
    "city desc", Comparator.comparing(CustomObject::getCity, Comparator.reverseOrder()),
    "zip", Comparator.comparingInt(CustomObject::getZipCode),
    "zip desc", Comparator.<CustomObject>comparingInt(CustomObject::getZipCode).reversed()
);

Then just get appropriate comparator by its alias:

List<CustomObject> data = Arrays.asList(
    new CustomObject(1, "Jack", "London", 23456),
    new CustomObject(9, "Buck", "Boston", 45678),
    new CustomObject(5, "Kyle", "Jersey", 19876),
    new CustomObject(7, "Stan", "Ashton", 36901)
);

System.out.println("raw list:  " + data);

data.sort(comparators.get("id desc"));
System.out.println("id desc:   " + data);

data.sort(comparators.get("name desc"));
System.out.println("name desc: " + data);

data.sort(comparators.get("zip"));
System.out.println("zip code:  " + data);

Output:

raw list:  [{id=1, name=Jack, city=London, zip=23456}, {id=9, name=Buck, city=Boston, zip=45678}, {id=5, name=Kyle, city=Jersey, zip=19876}, {id=7, name=Stan, city=Ashton, zip=36901}]
id desc:   [{id=9, name=Buck, city=Boston, zip=45678}, {id=7, name=Stan, city=Ashton, zip=36901}, {id=5, name=Kyle, city=Jersey, zip=19876}, {id=1, name=Jack, city=London, zip=23456}]
name desc: [{id=7, name=Stan, city=Ashton, zip=36901}, {id=5, name=Kyle, city=Jersey, zip=19876}, {id=1, name=Jack, city=London, zip=23456}, {id=9, name=Buck, city=Boston, zip=45678}]
zip code:  [{id=5, name=Kyle, city=Jersey, zip=19876}, {id=1, name=Jack, city=London, zip=23456}, {id=7, name=Stan, city=Ashton, zip=36901}, {id=9, name=Buck, city=Boston, zip=45678}]

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42