0

I'm trying to convert and order a set of strings.

The Model:

public class PartnerSearchResultDTO
{
    public int Id { get; set; }
    public string Distance { get; set; }
    public int Classification { get; set; }
}

Where I have for example a set:

IList<PartnerSearchResultDTO> partnersDTO = new List<PartnerSearchResultDTO>();
partnersDTO = 

Id = 1 | Distance = "2,7" | Classification = 1
Id = 2 | Distance = "5"   | Classification = 1
Id = 3 | Distance = "4,3" | Classification = 1
Id = 4 | Distance = "5,2" | Classification = 1
Id = 5 | Distance = "8"   | Classification = 1

And I order like this:

partnersDTO = partnersDTO.ToList().OrderBy(p =>  double.Parse(p.Distance)).ToList();

If the Browser's language is set to Portugues "pt-PT", it order correctly:

2,7 -> 4,3 -> 5 -> 5,2 -> 8

If the Browser's language is set to English "en-us", it ignores the comma and orders like this:

5 -> 8 -> 2,7 -> 4,3 -> 5,2
Patrick
  • 2,995
  • 14
  • 64
  • 125
  • 2
    Yeah so change `double.Parse()` to something that can handle commas regardless of the current culture. See duplicate. – CodeCaster May 06 '15 at 10:15
  • 1
    For your example the easiest solution would be to replace *double.Parse(p.Distance)* with *double.Parse(p.Distance, CultureInfo.GetCultureInfo("pt-PT"))* that will always treat comma as a decimal separator. – kaarel May 06 '15 at 10:19
  • Hi @kaarel great! It solved the problem, can you please post your answer so I can set it has correct? – Patrick May 06 '15 at 10:29
  • @Patrick As the question has been marked as duplicate, no answers can be added at this time. Glad it worked. – kaarel May 06 '15 at 10:31
  • Ok @kaarel but the duplicated question don't present a solution for this specific case, but thank you for your help :) – Patrick May 06 '15 at 10:33

0 Answers0