List T OrderBy Alphabetical Order
If you mean an in-place sort (i.e. the list is updated):
people.Sort((x, y) => string.Compare(x.LastName, y.LastName));
If you mean a new list:
var newList = people.OrderBy(x=>x.LastName).ToList(); // ToList optional
Sort a list alphabetically
You can sort a list in-place just by calling List<T>.Sort
:
list.Sort();
That will use the natural ordering of elements, which is fine in your case.
EDIT: Note that in your code, you'd need
_details.Sort();
as the Sort
method is only defined in List<T>
, not IList<T>
. If you need to sort it from the outside where you don't have access to it as a List<T>
(you shouldn't cast it as the List<T>
part is an implementation detail) you'll need to do a bit more work.
I don't know of any IList<T>
-based in-place sorts in .NET, which is slightly odd now I come to think of it. IList<T>
provides everything you'd need, so it could be written as an extension method. There are lots of quicksort implementations around if you want to use one of those.
If you don't care about a bit of inefficiency, you could always use:
public void Sort<T>(IList<T> list)
{
List<T> tmp = new List<T>(list);
tmp.Sort();
for (int i = 0; i < tmp.Count; i++)
{
list[i] = tmp[i];
}
}
In other words, copy, sort in place, then copy the sorted list back.
You can use LINQ to create a new list which contains the original values but sorted:
var sortedList = list.OrderBy(x => x).ToList();
It depends which behaviour you want. Note that your shuffle method isn't really ideal:
- Creating a new
Random
within the method runs into some of the problems shown here - You can declare
val
inside the loop - you're not using that default value - It's more idiomatic to use the
Count
property when you know you're working with anIList<T>
- To my mind, a
for
loop is simpler to understand than traversing the list backwards with awhile
loop
There are other implementations of shuffling with Fisher-Yates on Stack Overflow - search and you'll find one pretty quickly.
C# Sorting alphabetical order a - z and then to aa, ab - zz
This should do it.
var data = new List<string>() { "a", "b", "f", "aa", "z", "ac", "ba" };
var sorted = data.OrderBy(x => x.Length).ThenBy(x => x);
Result:
a, b, f, z, aa, ac, ba
Linq Order by alphabetical
This query
var product = Db.tablename
.Where(s => s.colum == DropDownList2.SelectedValue)
.OrderBy(s=> s.Name);
will not be executed until it is asked to. So you have to change it to the following one:
var product = Db.tablename
.Where(s => s.colum == DropDownList2.SelectedValue)
.OrderBy(s=> s.Name).ToList();
The reason why that happens is that actually you just have declared a query. I mean you haven't executed it. That's the nature of LINQ queries, which in technical terms is called deffered execution. On the other hand if you call the ToList()
method at the end of your query, you will trigger the immediate execution of this query and it's result will be a List
of the same type with s.Name
.
C# List OrderBy Alphabetical ABCABC NOT AABBCC
Not saying this is the most efficient way to do it but it works:
List<DrawingData> _DrawingList = new List<DrawingData>();
_DrawingList.Add(new DrawingData() { DrawingName = "411000D", DrawingQty = 1 });
_DrawingList.Add(new DrawingData() { DrawingName = "411000D", DrawingQty = 1 });
_DrawingList.Add(new DrawingData() { DrawingName = "411000A", DrawingQty = 1 });
_DrawingList.Add(new DrawingData() { DrawingName = "411000A", DrawingQty = 1 });
_DrawingList.Add(new DrawingData() { DrawingName = "411000C", DrawingQty = 1 });
_DrawingList.Add(new DrawingData() { DrawingName = "411000C", DrawingQty = 1 });
_DrawingList.Add(new DrawingData() { DrawingName = "411000B", DrawingQty = 1 });
_DrawingList.Add(new DrawingData() { DrawingName = "411000B", DrawingQty = 1 });
var _WithIndex = _DrawingList.Select(x => new { DrawingData = x, Index = _DrawingList.Where(y => y.DrawingName == x.DrawingName).ToList().IndexOf(x) });
var _FinalOrder = _WithIndex.OrderBy(x => x.Index).ThenBy(x => x.DrawingData.DrawingName).Select(x => x.DrawingData);
Console.WriteLine("Final Sort:");
Console.WriteLine(string.Join("\n", _FinalOrder));
Console.ReadLine();
Get the index of each duplicated item, then sort on that index and then the name.
Made it a bit simpler. Can be a single LINQ statement:
var _FinalOrder = _DrawingList
.Select(x => new
{
DrawingData = x,
Index = _DrawingList.Where(y => y.DrawingName == x.DrawingName)
.ToList()
.IndexOf(x)
})
.OrderBy(x => x.Index)
.ThenBy(x => x.DrawingData.DrawingName)
.Select(x => x.DrawingData);
Sorting a List with OrderBy
since OrderBy returns IOrderedEnumerable you should do:
lst = lst.OrderBy(p => p.Substring(0)).ToList();
you can also do the following:
lst.Sort();
Sort a list alphabetically excluding a letter
You can use any expression that returns a sortable value as a sort order; it doesn't have to be a property. To put all objects whose name starts with "D" at the end of the list just use:
.OrderBy(o => o.Name.StartsWith("D") ? 1 : 0)
.ThenBy(o => o.Name)
How to Sort a List T by a property in the object
The easiest way I can think of is to use Linq:
List<Order> SortedList = objListOrder.OrderBy(o=>o.OrderDate).ToList();
Related Topics
Simple State MAChine Example in C#
The Type or Namespace Name Does Not Exist in the Namespace 'System.Web.Mvc'
How to Use Non-Thread-Safe Async/Await APIs and Patterns with ASP.NET Web API
Difference Between Parameters.Add(String, Object) and Parameters.Addwithvalue
How to Make Two Transparent Layer with C#
Get Month Name from Month Number
Sort Observablecollection<String> Through C#
What's the Difference Between Bitmap.Clone() and New Bitmap(Bitmap)
What Does Placing a @ in Front of a C# Variable Name Do
Which Cryptographic Hash Function Should I Choose
Delegates: Predicate VS. Action VS. Func
How to Implement a Never Ending Task. (Timers VS Task)
What Happens If a Finally Block Throws an Exception
C# Rsa Encryption/Decryption with Transmission
Unique Random String Generation
How to Get the Groups of a User in Active Directory? (C#, ASP.NET)