To sort a list of objects by a string property in C# where empty strings are placed last, you can use LINQ's OrderBy method with a custom sorting logic. The key idea is to consider empty strings as greater than any non-empty string when sorting.
Here is an example:
Let's consider a class Person with a Name property:
using System; using System.Collections.Generic; using System.Linq; public class Person { public string Name { get; set; } } public class Program { public static void Main() { // Create a list of Person objects var people = new List<Person> { new Person { Name = "Alice" }, new Person { Name = "" }, new Person { Name = "Bob" }, new Person { Name = "Charlie" }, new Person { Name = "" }, new Person { Name = "Dave" } }; // Sort the list with empty strings last var sortedPeople = people.OrderBy(p => string.IsNullOrEmpty(p.Name) ? 1 : 0) .ThenBy(p => p.Name) .ToList(); // Print the sorted list foreach (var person in sortedPeople) { Console.WriteLine($"Name: '{person.Name}'"); } } } Person Class: Defines a simple class with a Name property.
public class Person { public string Name { get; set; } } Creating the List: A list of Person objects is created, some with empty Name properties.
var people = new List<Person> { new Person { Name = "Alice" }, new Person { Name = "" }, new Person { Name = "Bob" }, new Person { Name = "Charlie" }, new Person { Name = "" }, new Person { Name = "Dave" } }; Sorting the List: The list is sorted using LINQ's OrderBy and ThenBy methods.
OrderBy(p => string.IsNullOrEmpty(p.Name) ? 1 : 0): This will place items with empty strings at the end because 1 is considered greater than 0.ThenBy(p => p.Name): This will sort the remaining non-empty names alphabetically.var sortedPeople = people.OrderBy(p => string.IsNullOrEmpty(p.Name) ? 1 : 0) .ThenBy(p => p.Name) .ToList();
Printing the Sorted List: The sorted list is printed to verify the order.
foreach (var person in sortedPeople) { Console.WriteLine($"Name: '{person.Name}'"); } The output will be:
Name: 'Alice' Name: 'Bob' Name: 'Charlie' Name: 'Dave' Name: '' Name: ''
In this way, all empty strings are placed at the end of the sorted list. This approach can be adapted to other scenarios where you need custom sorting logic in C#.
C# sort objects by string property, empty string last Description: How to sort a list of objects by a string property where empty strings are placed last.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty)).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() methods to sort list by YourStringProperty, placing items with empty strings (null or "") last.C# sort list by string property ascending empty string Description: How to sort a list of objects by a string property in ascending order, with empty strings at the end.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty)).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() methods to sort list by YourStringProperty, ensuring empty strings are positioned last.C# sort objects by string property descending empty string Description: How to sort a collection of objects by a string property in descending order, with empty strings last.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderByDescending(x => string.IsNullOrEmpty(x.YourStringProperty)).ThenByDescending(x => x.YourStringProperty).ToList();
OrderByDescending() and ThenByDescending() methods to sort list by YourStringProperty in descending order, ensuring empty strings are placed last.C# sort list by string property empty string last Description: How to sort a list of objects by a string property with empty strings appearing at the end.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty) ? 1 : 0).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() to sort list by YourStringProperty, ensuring items with empty strings are positioned last.C# sort objects by property empty string last LINQ Description: How to use LINQ to sort objects by a string property, placing empty strings last.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty)).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() methods to list, sorting by YourStringProperty and ensuring empty strings are last.C# orderby string property empty string Description: How to use OrderBy to sort objects by a string property, ensuring empty strings are placed last.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty)).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() LINQ methods to sort list by YourStringProperty, ensuring empty strings come last.C# sort by string property empty string last Description: How to sort objects by a string property, ensuring empty strings are last, using LINQ.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty)).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() methods to sort list by YourStringProperty, ensuring items with empty strings are placed at the end.C# order by string property empty string last Description: How to order a list of objects by a string property, with empty strings appearing last.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty) ? 1 : 0).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() to sort list by YourStringProperty, ensuring empty strings are placed last.C# sort list by string property empty string at end Description: How to sort a list by a string property, ensuring empty strings are at the end of the sorted result.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty) ? 1 : 0).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() LINQ methods to sort list by YourStringProperty, ensuring empty strings are positioned last.C# sort objects by string property null or empty last Description: How to sort objects by a string property, ensuring both null and empty strings are placed at the end.
List<YourClass> list = new List<YourClass>(); // Assume list is populated with objects list = list.OrderBy(x => string.IsNullOrEmpty(x.YourStringProperty)).ThenBy(x => x.YourStringProperty).ToList();
OrderBy() and ThenBy() LINQ methods to sort list by YourStringProperty, ensuring both null and empty strings are last in the sorted result.web npapi windows-server-2012 virtual-keyboard single-sign-on excel-2011 postgis ibm-cloud tensorflow2.x filefield