Ok I think I found out where the problem comes from. I made a simple test in SQL Server with my two tables Employee and Department.
Employee as 3 fields: empId, empName and depId.
Department as 2 fields: depId and depName
If I do the following:
select Employee.empName
from Employee left join Department on Department.depId = Employee.depId
order by Department.depName
it sorts the employees by their respective department name in ascending order -> FINE.
However, if I try this one:
select Employee.empName as depName
from Employee left join Department on Department.depId = Employee.depId
order by Department.depName
then it sorts everything out by the employee name!
In my real case, the Department name column is only labeled Name in my database and in my entity the Employee Name column is renamed to Name: so the DQE generates the following query:
SELECT [dbo].[Employee].[empName] AS [Name]
FROM [dbo].[Employee] LEFT JOIN [dbo].[Department] ON [dbo].[Department].[depId] = [dbo].[Employee].[depId]
ORDER BY [dbo].[Department].[Name]
and I fall exactly in the second case and that's why it doesn't sort properly. If in the above query I just change the alias 'AS [Name]' to 'AS [EmployeeName]', everything works fine.