1
I have a List with attributes, of which I have the field of reviews, containing: Excellent, Good, Regular, Bad, and Bad. The information for this field I get from the consumption of a REST API using JSON.
Example:
IEnumerable<minhaEntidade> minhaLista = _respositorio.ConsumirAPI();
foreach (var item in minhaLista)
{
Retorno.Add(new minhaNovaEntidade()
{
Nome = item.NomeCompleto,
Endereco = item.EnderecoCompleto,
Avaliacao = item.AvaliacaoDesempenho
});
}
If I do:
return Retorno.OrderBy(x => x.Avaliacao);
I will have the list sorted by the performance evaluations in alphabetical order.
However, I need to sort the list by following the precedence criterion: Excellent, Good, Regular, Bad, and Bad.
That is, all Names and Addresses that were first evaluated as Excellent, then Good, then Regular, then Bad, then Bad. In this order.
I’ve tried to create a Type-Safe Enum Pattern to compare the obtained list with my Type-Safe Enumerator, example:
return Retorno.OrderBy(x => MeuEnumerador().CompareTo(x.Avaliacao));
or...
return Retorno.Sort((x,y) => x.Avaliacao.CompareTo(MeuEnumerador().ToString()))
But it didn’t work and/or make a mistake.
Questions:
Has anyone ever needed to do something similar to this problem of mine using C#?
Or even, have any hint of implementation for what I’m looking for?
Remembering that the information does not come from a database (which would be easy to solve), but from a JSON API, and the field is just a string.
What kind of
Avaliacao
?– Jéf Bueno
@LINQ the Rating field is string type. And when I consume the API, it comes Excellent, or Good, or Regular, or Bad, or Bad.
– Alexandre Dórea
IEnumerable<minhaEntidade> minhaLista
in this variable already has the list of values, is why you need to order?– novic
@Virgilionovic This is exactly the list I need to sort by the Evaluation field, which is string type, but not alphabetically. First sort all the cases that were evaluated by Excellent, then Good and so on.
– Alexandre Dórea
I understand Alexandre Dórea.
– novic