# OrderBy Linq example

There will come a time when you need to order an array or list of objects based on a particular property. When using the OrderBy method, by default it's ascending order.

# A basic example

We can use OrderBy with simple types such as integers and strings. With the example below, we will be using strings.

# Method syntax

Below we have a list of cities that we want to order the cities alphabetically. The OrderBy default of ordering is by ascending order. For ordering in descending, we will need to use OrderbyDescending.

Live example: https://dotnetfiddle.net/HOVtKF

var cities = new[] {"Barcelona", "London", "Paris", "New York", "Moscow", "Amsterdam", "Tokyo", "Florence"};

var orderedCities = cities.OrderBy(s => s);

//output: Amsterdam, Barcelona, Florence, London, Moscow, New York, Paris, Tokyo

# Query syntax

Similar to the method syntax above, for the query syntax, the orderby keyword is used.

Live example: https://dotnetfiddle.net/3fVp64

var cities = new[] {"Barcelona", "London", "Paris", "New York", "Moscow", "Amsterdam", "Tokyo", "Florence"};

var orderedCities = from city in cities
                    orderby city        // orderby keyword
                    select city;

//output: Amsterdam, Barcelona, Florence, London, Moscow, New York, Paris, Tokyo