0

I'm new to this and I need to sort my data. I have defects with an array, called priorities, that has some names in it. Now I want to sort them in descending order based on the length of the array. I'm struggling and can't seem to find how to do this. This is my code:

@GetMapping("/defects/sort/priority")
public List<Defect> defects2() { return defectRepository.sortAllDefectByPriority(); }

also:

@Query("{ $sort: { $size: '$priorities' }: -1 }")
public List<Defect> sortAllDefectByPriority();

Can anyone help me? EDIT: I only know how to use @Query annotations

Rultar
  • 5
  • 4
  • https://stackoverflow.com/questions/9040161/mongo-order-by-length-of-array – upog Apr 21 '20 at 09:31
  • Within the query – Rultar Apr 21 '20 at 09:54
  • To calculate the size of an array you have to use the `$size` aggregation array operator. So, I am not sure you can use the `@Query`, but you can use the `@Aggregation` and get the needed result. – prasad_ Apr 21 '20 at 09:57
  • So you would change it to: `@Aggregation("{ $sort: { $size: '$priorities' }: -1 }") public List sortAllDefectByPriority();`? – Rultar Apr 21 '20 at 10:51

0 Answers0