C# List<> Sort by X Then Y

C# List Sort by x then y

Do keep in mind that you don't need a stable sort if you compare all members. The 2.0 solution, as requested, can look like this:

 public void SortList() {
MyList.Sort(delegate(MyClass a, MyClass b)
{
int xdiff = a.x.CompareTo(b.x);
if (xdiff != 0) return xdiff;
else return a.y.CompareTo(b.y);
});
}

Do note that this 2.0 solution is still preferable over the popular 3.5 Linq solution, it performs an in-place sort and does not have the O(n) storage requirement of the Linq approach. Unless you prefer the original List object to be untouched of course.

Sort a 2D Points List (first by X and then Y)

This should work better:

class PointComparer : IComparer<Point>
{
public int Compare(Point first, Point second)
{
if (first.X == second.X)
{
return first.Y - second.Y;
}
else
{
return first.X - second.X;
}

}
}

If the X values are different, it will use the Y value for sorting. This is different from your code, where X values will be used if the Y values are the same.

As others have mentioned, if you can use Linq, you should use the OrderBy and ThenBy extension methods:

pointsList.OrderBy(p => p.X).ThenBy(p => p.y)

Sort a List and keep a particular element at end of list after sorting

This is a fine answer, but I thought I would fix your comparer:

Test:

[TestCase(new string[0], new string[0])]
[TestCase(new[] { "a" }, new[] { "a" })]
[TestCase(new[] { "a", "b" }, new[] { "a", "b" })]
[TestCase(new[] { "b", "a" }, new[] { "a", "b" })]
[TestCase(new[] {"others"}, new[] {"others"})]
[TestCase(new[] {"a", "others"}, new[] {"a", "others"})]
[TestCase(new[] {"others", "a"}, new[] {"a", "others"})]
[TestCase(new[] {"others", "x"}, new[] {"x", "others"})]
[TestCase(new[] {"Others", "x"}, new[] {"x", "Others"})]
[TestCase(new[] { "othersz", "others" }, new[] { "othersz", "others" })]
[TestCase(new[] {"z", "y", "x", "others", "b", "a", "c"},
new[] {"a", "b", "c", "x", "y", "z", "others"})]
public void CanSortWithOthersAtEnd(string[] input, string[] expectedSorted)
{
var a = new List<string>(input);
var c = new EOComparer();
a.Sort(c.Compare);
CollectionAssert.AreEqual(expectedSorted, a);
}

Comparer:

public sealed class EOComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (IsOthers(x)) return 1;
if (IsOthers(y)) return -1;
return string.Compare(x, y, StringComparison.Ordinal);
}

private static bool IsOthers(string str)
{
return string.Compare(str, "others", StringComparison.OrdinalIgnoreCase) == 0;
}
}

Note how my use of string.Compare avoids all == null checks. And how StringComparison.OrdinalIgnoreCase avoids .ToLower() and thus avoids creating copies of the strings.

How to sort List Point

LINQ:

pointsOfList = pointsOfList.OrderByDescending(p => p.X).ToList();

C# list sort by two columns

result.Sort((x,y) => x.CheckedIn==y.CheckedIn ? 
string.Compare(x.LastName, y.LastName) :
(x.CheckedIn ? -1 : 1) );

C# Sorting a list by X and Y

C# is case sensitive. You need

// Note the capital letters
List<SomeClass> b = a.OrderBy(item => item.X).ThenBy(item => item.Y).ToList();

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();

Sorting a List in C# using List.Sort(Comparison T comparison

You can use Linq OrderBy method for that -

sm = sm.OrderBy(i => i.num_of_words).ToList();

Sorting a List String containg coordinates x and y

OrderBy and ThenBy do not modify the original list, they only return a new list (in the form of an IEnumerable<>). What you need to do is create a new List<> from the resulting IEnumerable<>, like this:

// Note that we are assigning the variable to a new list
boardObjectList = boardObjectList.OrderBy(p => (p.Split())[0])
.ThenBy(p => (p.Split())[1])
.ToList(); // Also note that we call ToList,
// to get a List from an IEnumerable

You will get strange results when storing numbers in strings, and trying to sort. I recommend changing your code to this:

boardObjectList = boardObjectList.OrderBy(p => int.Parse(p.Split()[0]))
.ThenBy(p => int.Parse(p.Split()[1]))
.ToList();

This method converts the strings into integers before sorting. The reason to do this is that string sorting sorts alphabetically, leading to sorting like this:

1
10
11
12
2
3
4
5
6
7
8
9

Sorting List String in C#

How about:

    list.Sort((x, y) =>
{
int ix, iy;
return int.TryParse(x, out ix) && int.TryParse(y, out iy)
? ix.CompareTo(iy) : string.Compare(x, y);
});


Related Topics



Leave a reply



Submit