Sorting an Ilist in C#

Sorting an IList in C#

How about using LINQ To Objects to sort for you?

Say you have a IList<Car>, and the car had an Engine property, I believe you could sort as follows:

from c in list
orderby c.Engine
select c;

Edit: You do need to be quick to get answers in here. As I presented a slightly different syntax to the other answers, I will leave my answer - however, the other answers presented are equally valid.

Sorting IListSpecific_Object_Type

You need to add: using System.Linq;

IList<keyword> sortedList = list.OrderBy(r => r.key).ToList();

Or you can try:

  IList<keyword> sortedList2 = (from r in list
orderby r.key
select r).ToList();

How to sort an iList (With linq or without)

You should use the generic form of IList to be able to use the LINQ extension methods:

private void AddListToTree<T>(ComponentArt.Web.UI.TreeView treeView,
IList<T> list)
{
var orderedList = list.OrderBy(t => t);
// ...
}

If you can't modify the method's signature but you know the type of the objects in the IList, you can use Cast:

private void AddListToTree(ComponentArt.Web.UI.TreeView treeView,
IList list)
{
var orderedList = list.Cast<SomeType>().OrderBy(x => x);
// ...
}

How Do I Sort IListClass?

Use OrderBy

Example

public class MyObject() 
{
public int number { get; set; }
public string marker { get; set; }
}

IList<MyObject> myobj = new List<MyObject>();
var orderedList = myobj.OrderBy(x => x.marker).ToList();

For a case insensitive you should use a IComparer

public class CaseInsensitiveComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
}
}

IList<MyObject> myobj = new List<MyObject>();
var orderedList = myobj.OrderBy(x => x.marker, new CaseInsensitiveComparer()).ToList();

How to sort an IList in ascending or descending order Selenium C#

You need to assign the result of OrderBy to another list, it doesn't affect the list you call it on. Like so:

var sortedList = tableColumnData.OrderBy(t => t.Text); 
foreach (IWebElement element in sortedList)
{
Console.WriteLine(element.Text);
}

Sorting an IList by number in C#

You should convert this value to a numeric type. For example, if your data allows it, an integer.

List<TestGrid> sortedList = myList.OrderBy(r => Convert.ToInt32(r.ActivOrderNo)).ToList();

At the moment, your sorting is happening on the string "12" vs "1" but it sounds like you actually want to sort on the value of the number inside the string.

How do I sort IList C # by date?

I think this is an X/Y problem. You're trying to solve a complex "sort two lists congruent to each-other". And ... you can do that, but it is very hard. Array.Sort has a suitable method for sorting two arrays in connection, but: not lists. You could try and do that, but frankly I think you're solving the wrong problem. If each DateTime and double is logically connected, then rather than having two lists: have one list with a composite value. For example:

var list = new List<(DateTime when, double value)>();
list.Add((dateX, valueX)); // your values...
list.Add((dateY, valueY));
list.Add((dateZ, valueZ));

list.Sort((x,y) => x.when.compareTo(y.when));

Here I'm using tuple-types, but the same thing can be done by declaring your own type to hold the composite values. You might also want to look into pre-sorted list container types, but that places more demands on the key value (uniquity, etc).

Sort and IList of numbers in ascending order c#

You can simply use Linq for the task:

var list = new List<int>() { -5, 8, -7, 0, 44, 121, -7 };
var sorted = list.OrderBy(x => x);

Ordering items in IListT

I assume that you have three classes that implement IRule (AddRule, EditRule, DeleteRule).

If you can change the type of allRules from IList to List, you can use the List<T>.Sort(Comparison<T>) method. Comparison is a generic delegate with the signature

public delegate int Comparison<in T>(T x,T y)

so you'll need something like this:

public int IRuleComparer(IRule first, IRule second)
{
//build a table of type weights (this could be made static)
Dictionary<Type, int> typeWeights = new Dictionary<Type, int>();
typeWeights.Add(typeof(AddRule), 1);
typeWeights.Add(typeof(EditRule), 2);
typeWeights.Add(typeof(DeleteRule), 3);

//get the types of the arguments
Type firstType = first.GetType();
Type secondType = second.GetType();

//are the types valid?
if (!typeWeights.ContainsKey(firstType))
throw new Exception("invalid first type");

if (!typeWeights.ContainsKey(secondType))
throw new Exception("invalid second type");

//compare the weights of the types
return typeWeights[firstType].CompareTo(typeWeights[secondType]);
}

Also, take note that the Sort implementation uses the quick sort algorithm, that is not a stable sort, i.e it might mess the relative order of AddRules, so in your example, AddRule2 could get sorted before AddRule1.


Alternatively, you could use LINQ with something like this:

public int GetRuleWeight(IRule item)
{
//build a table of type weights (this could be made static)
Dictionary<Type, int> typeWeights = new Dictionary<Type, int>();
typeWeights.Add(typeof(AddRule), 1);
typeWeights.Add(typeof(EditRule), 2);
typeWeights.Add(typeof(DeleteRule), 3);

Type itemType = item.GetType();

if (!typeWeights.ContainsKey(itemType))
throw new Exception("invalid type");

return typeWeights[itemType];
}

allRules = allRules.OrderBy(item => GetRuleWeight(item)).ToList();

this will work with IList (even with IEnumerable), so you wont have to change the type of allRules.



Related Topics



Leave a reply



Submit