4 Replies - 684 Views - Last Post: 18 September 2012 - 04:19 PM Rate Topic: -----

#1 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Alphabetising words with special characters into a list

Posted 18 September 2012 - 07:38 AM

A website I'm working on has a select list that a user uses to pick the country they are from.
This is done by a simple countriesNonOrdered.Orderby(cno => cno.Text) and works as expected.

But now the site is going to have the same options in french, which means that words with special characters like Émirats arabes unis (United Arab Emirates) appears at the top of the options, when it needs to be with all the other countries that begin with E.

My question is, how can I order the list of countries alphabetically when there are now these words with special characters?

Thanks for any help :)

This post has been edited by DivideByZero: 18 September 2012 - 07:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Alphabetising words with special characters into a list

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,818
  • Joined: 02-June 10

Re: Alphabetising words with special characters into a list

Posted 18 September 2012 - 07:46 AM

The crude way would be just don't use the special form of the E.
I think you're users will still recognize
Emirats arabes unis if it doesn't have the special accent mark.

The second way would be to use the overload of .OrderBy that makes use of the ICompare interface and write your own ICompare implimentation.

http://msdn.microsof...y/bb549422.aspx

http://msdn.microsof....icomparer.aspx
Was This Post Helpful? 4
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4485
  • View blog
  • Posts: 7,813
  • Joined: 08-June 10

Re: Alphabetising words with special characters into a list

Posted 18 September 2012 - 07:46 AM

*
POPULAR

I just tested this, and it seems to do this already, just by using OrderBy. However, we're in different countries, we might be using different culture rules for sorting.

Try this:

...countriesNonOrdered.Orderby(cno => cno.Text, StringComparer.InvariantCulture)...


Edit, beaten to the punch. I'd suggest against the first, since as you said, it's crude. Always do your best to respect the culture that will be using your software.

Also, the static class StringComparer provides a few premade IComparers that are quite useful: CurrentCulture, CurrentCultureIgnoreCase, InvariantCulture, InvariantCultureIgnoreCase, Ordinal, OrdinalIgnoreCase.

InvariantCulture should be good here. Ordinal is good for passwords. OrdinalIgnoreCase is good for comparing input against a list of possible commands, etc (and is a great way to avoid comparisons like x.ToUpper() == y.ToUpper()).
Was This Post Helpful? 5
  • +
  • -

#4 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Alphabetising words with special characters into a list

Posted 18 September 2012 - 08:12 AM

The StringComparer.InvariantCulture overload works perfectly, thanks :)
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

Re: Alphabetising words with special characters into a list

Posted 18 September 2012 - 04:19 PM

Although InvariantCulture worked for you, note that the primary purpose of that is for internal or back-end use much like using Ordinal or OrdinalCase.

In your OP you said:

Quote

A website I'm working on has a select list that a user uses to pick the country they are from.


For things which are user facing, you should respect the users configurations and settings. I can't tell you how much it drives me crazy when a choose a particular date and time format in Windows and programs just ignore my settings because "they know better". You might as well change my fonts, colors, and wallpapers while you are at it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1