You’re right – this query of yours will do that:
SELECT users.username
, user_custom_fields.value
FROM users
JOIN user_custom_fields
ON users.id = user_custom_fields.user_id
OR users.id = CAST( user_custom_fields.value AS int )
WHERE user_custom_fields.name LIKE 'shadow_id'
ORDER BY user_custom_fields.value
There is no obvious way through the UI though.