Is there any way to make those functions getting the results ordered like in the order of the list (or collection) given to the function for the in lookup?
Simple example of a spring data repository interface function:
public void List<Entity> findByColorIn(List<String> colors)
Now im creating a string list with the order "green", "blue", "cyan" and calling this function.
Assuming there is a unique match of all these three colors:
how can i make the function returning the results by the given order of property in the given list? In this example:
Entity[id=32, color="green"]
Entity[id=11, color="blue"]
Entity[id=22, color="cyan"]
Not sure whats the default order in this case but im assuming the id...
spring data has two variants with api how you can use standard order based on property.
1 variant :
public void List<Entity> findByColorInOrderByColorDesc(List<String> colors)
public void List<Entity> findByColorInOrderByColorAsc(List<String> colors)
2 variant
Sort sort= new Sort(Sort.Direction.ASC/DESC,"color");
public void List<Entity> findByColorIn(List<String> colors , Sort sort)
if you want to use custom order for case , sort order :
color="green" , color="blue", color="cyan"
you need use variant 2 with custom Sort implementation based on your sort logic.
Also you can get not sorted result and sort it in server side if you have small result set . spring data might return stream , so you can do something like :
findByColorIn(colors).stream().sorted(comparator....).collect(Collectors.toList());
In my case I just sorted it by hand since I wasn't dealing with a very long array of items.
First you retrieve the elements from database in whatever order they come, then compare with your color list and sort it accordingly.