How will you keep a specific country at the top of the drop-down list?

In the list of countries that should be displayed in alphabetical order, you need to place the USA at the top of the list. How do you do this?

I replied by saying that I would have a table structure so that the US would have id-0. The remaining countries are listed in alphabetical order.

So when I exit the table, I will do " SELECT CountryName from tableOfCountries ORDER by ID "

I'm not sure the interviewer wanted to hear that. Therefore, I proposed another option for populating an array of countries from the USA as the first element. Then the remaining elements will be populated from the query result set.

 "SELECT CountryName FROM tableOfCountries WHERE countryName != 'US' ORDER by COUNTRY_NAME". 

This ensures that the USA will not be selected twice.

The interviewer was not satisfied with this option. So he asked me if I have another option. Then I said a text file on a web server with a list of values.

Do you have other options you might think about?

+7
c # oracle
source share
7 answers

Generally something like:

 SELECT CountryName from tableOfCountries ORDER by (case when CountryName = 'US' then 0 else 1 end) asc, CountryName asc 
+8
source share

Another column in the country table has priority.

Assign the United States a priority greater than 1 and leave all other countries equal to 0.

Your SQL will look like this:

 select Name from countries order by precedence desc, name asc 

This will allow you to scale this later if necessary.

+10
source share

May be:

 select name from countries order by case when name = 'US' then 1 else 2 end, name; 
+3
source share

Of course, I would not abuse the identifier if I could avoid it.

You can prioritize countries, and then:

 select isoCode, name from countries order by priority desc, name 

Alternatively, why is this up? I would suggest changing the logic so that they remain in alphabetical order, but the US was selected by default before the change.

+3
source share

Another way to do this is to get a list of states and then filter them in a dataview, excluding the default elements. Then set the list management property AppendDataBoundItems to true. Add your items to the list control and then databind in the dataview.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx

This will cause you to not clear your default values ​​when a data binding occurs.

+1
source share

Hi, I found this article, hope this helps you clear the questions: http://www.nexustechnologiesllc.com/blog/fixing-registration-forms-country-selection/

I'm new, I apologize if I did not answer the question in the correct format

+1
source share

If it is in C #, do not consider how the data is returned from the server, placing it in a specific type and implementing IComparable for this type. Then the drop-down list will hold them accordingly, as you can indicate in the CompareTo method that the US type is β€œlarge” from other countries.

 class Country : IComparable<Country> { int IComparable<Country>.CompareTo(Country other) { <US logic> else return String.Compare(this.CountryName, other.CountryName); } } 

EDIT: This is actually in C # based on OP tags. Why the focus on the back end for the presentation happens outside of me.

-one
source share

All Articles