Make a select in database

Hi,
what would be the best way to achieve this please ?
I want to sort a list regarding an algorithm so I need use many lines of code for to weight certain data
example: if A = B then A has priority and is in top of the list
Not only with A and B and with date also …
An external backend with stored procedure feature ?
Thanks