sorting
-
Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
-
Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
Write a comparer that does what you want and use Sort?
-
Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
using System.Linq; public class record { public string name1; public string name2; } List records = new List(); records.Add(new record() { name1 = "a", name2 = "b" }); records.Add(new record() { name1 = "c", name2 = "d" }); records.Add(new record() { name1 = "e", name2 = "f" }); records.Add(new record() { name1 = "g", name2 = "h" }); records.Add(new record() { name1 = "i", name2 = "j" }); var r = records.OrderByDescending(sort => sort.name1).OrderBy(sort2 => sort2.name2);;
-
Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
There are lots of ways of sorting the list: see here[^]. :) I would suggest you implement the IComparable<T>[^] interface and provide an appropriate body for
CompareTo
method. You can find such an example here[^].Ramkithepower wrote:
Now I have to sort this class first by name1 and then by name2.
In this case a possible implementation of the
CompareTo
method could be:public int CompareTo(Record other)
{
int res = name1.CompareTo(other.name1);if(res == 0)
res = name2.CompareTo(other.name2);return res;
} -
Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
var sortedData = records.OrderBy(r => r.name1 + r.name2);
You just concatenate the names in the key selector to achieve the ordering you want. Opps. PIEBALDconsult[^] is of course totally right. That was a very stupid suggestion of mine, I don't know what I was thinking. Please ignore the above suggestion. This is how you should really do it (using ThenBy[^]):
var sortedData = records.OrderBy(sort => sort.name1).ThenBy(sort2 => sort2.name2);
Simon
-
var sortedData = records.OrderBy(r => r.name1 + r.name2);
You just concatenate the names in the key selector to achieve the ordering you want. Opps. PIEBALDconsult[^] is of course totally right. That was a very stupid suggestion of mine, I don't know what I was thinking. Please ignore the above suggestion. This is how you should really do it (using ThenBy[^]):
var sortedData = records.OrderBy(sort => sort.name1).ThenBy(sort2 => sort2.name2);
Simon
I don't think that's reliable.
-
using System.Linq; public class record { public string name1; public string name2; } List records = new List(); records.Add(new record() { name1 = "a", name2 = "b" }); records.Add(new record() { name1 = "c", name2 = "d" }); records.Add(new record() { name1 = "e", name2 = "f" }); records.Add(new record() { name1 = "g", name2 = "h" }); records.Add(new record() { name1 = "i", name2 = "j" }); var r = records.OrderByDescending(sort => sort.name1).OrderBy(sort2 => sort2.name2);;
rvdenden wrote:
var r = records.OrderByDescending(sort => sort.name1).OrderBy(sort2 => sort2.name2);;
This isn't correct.
Because IOrderedEnumerable inherits from IEnumerable, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new primary ordering that ignores the previously established ordering.
You need to use ThenBy[^] for the second stage of the sort.
var r = records.OrderByDescending(sort => sort.name1).ThenBy(sort2 => sort2.name2);
Simon
-
I don't think that's reliable.
Why?
Simon
-
Hi, I have the following complex type, public class record() { string name1; string name2; } I create a list of records in another function public list records();//list of records Now I have to sort this class first by name1 and then by name2. Is there any simple way to do this?
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.
This article[^] covers the regular ways of sorting; it should be easy to understand and offer good performance at the same time; it does not offer the latest LINQ fancy things. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Why?
Simon
Unless you right-pad name1. Or are you counting on case-sensitivity?
-
var sortedData = records.OrderBy(r => r.name1 + r.name2);
You just concatenate the names in the key selector to achieve the ordering you want. Opps. PIEBALDconsult[^] is of course totally right. That was a very stupid suggestion of mine, I don't know what I was thinking. Please ignore the above suggestion. This is how you should really do it (using ThenBy[^]):
var sortedData = records.OrderBy(sort => sort.name1).ThenBy(sort2 => sort2.name2);
Simon
-
Unless you right-pad name1. Or are you counting on case-sensitivity?
Ohh... You are very right. Thanks. Will edit.
Simon
-
Why?
Simon
because it is wrong. consider the following set of string pairs:
a,bd
ab,cand try sorting them the right way and the concatenated way. It would be right if you were to use
string1 + charMax + string2
where charMax is a character that sorts last in the character collection. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
var sortedData = records.OrderBy(r => r.name1 + r.name2);
You just concatenate the names in the key selector to achieve the ordering you want. Opps. PIEBALDconsult[^] is of course totally right. That was a very stupid suggestion of mine, I don't know what I was thinking. Please ignore the above suggestion. This is how you should really do it (using ThenBy[^]):
var sortedData = records.OrderBy(sort => sort.name1).ThenBy(sort2 => sort2.name2);
Simon
Thank you all for the reply.
Jack Sparrow -------------------------------------- Defeat is not the worst of failures. Not to have tried is the true failure.