英文:
How to implement sorting on a list of dynamic strings with Name and Value
问题
我给这个问题取了一个标题,但希望解释足够清楚。
目前我有一个用于动态创建表格的对象。这个对象看起来像这样:
public class RowData
{
public string Name { get; set; }
public string Value { get; set; }
public RowData(string name, string value)
{
Name = name;
Value = value;
}
}
我有一个List<List<RowData>>
,我将其传回前端并在表格中填充。想法是它是一组行的列表,然后是每行的列数据的列表。
我想要实现一种方法,根据表格中选择的列对数据进行排序。我最初的想法是通过使用反射来对名称属性进行排序,然后根据其对应的值属性进行排序。
然而,我似乎陷入了困境...也许需要考虑更好的实现方式。
假设我有这样的数据添加到我的List<List<RowData>> rowData = new List<List<RowData>>();
中:
var col1 = new RowData() { Name = "Col1", Value = "123" };
var col2 = new RowData() { Name = "Col2", Value = "ABC" };
var col3 = new RowData() { Name = "Col3", Value = "Some Test" };
var col4 = new RowData() { Name = "Col1", Value = "456" };
var col5 = new RowData() { Name = "Col2", Value = "DEF" };
var col6 = new RowData() { Name = "Col3", Value = "Another Test" };
var row1 = new List<RowData>() { col1, col2, col3 };
var row2 = new List<RowData>() { col4, col5, col6 };
rowData.Add(row1);
rowData.Add(row2);
如果我传递一个字符串像Col1
,如何以编程方式按Name
等于Col1
的Value
对rowData
进行排序?还是我需要完全重新思考我的流程?
如果我传递了字符串Col1
,预期的结果将类似于:
Co11 | Col2 | Col3 |
---|---|---|
123 | ABC | Some Test |
456 | DEF | Another Test |
如果我传递了字符串Col3
,预期的结果将类似于:
Co11 | Col2 | Col3 |
---|---|---|
456 | DEF | Another Test |
123 | ABC | Some Test |
英文:
I gave my best shot at a title for this question, but hopefully the explanation will be enough.
Right now I have an object that gets populated for a dynamically created table. This object looks like this:
public class RowData
{
public string Name { get; set; }
public string Value { get; set; }
public RowData(string name, string value)
{
Name = name;
Value = value;
}
}
I have a List<List<RowData>>
that I pass back to my front end and get populated in a table. The idea being that it is a list of rows and then a list of each rows column data.
I am looking to implement a way to sort the data by the column selected in my table. My initial thought was that I would be able to achieve something like this by using reflection on the name property and then sorting by its corresponding value property.
However, I am seem to be stumped... and perhaps need to think about a better way to implement this.
Lets say I have data like so added into my List<List<RowData>> rowData = new List<List<RowData>>();
:
var col1 = new RowData() { Name = "Col1", Value = "123"};
var col2 = new RowData() { Name = "Col2", Value = "ABC" };
var col3 = new RowData() { Name = "Col3", Value = "Some Test" };
var col4 = new RowData() { Name = "Col1", Value = "456" };
var col5 = new RowData() { Name = "Col2", Value = "DEF" };
var col6 = new RowData() { Name = "Col3", Value = "Another Test" };
var row1 = new List<RowData>() { col1, col2, col3 };
var row2 = new List<RowData>() { col4, col5, col6 };
rowData.Add(row1);
rowData.Add(row2);
If I pass a string like Col1
how could a programmatically sort rowData
by Value
that are corresponds to Name
that equals Col1
? Or do I need to entirely rethink my process here?
If I passed the string of Col1
the anticipated outcome would be something like:
Co11 | Col2 | Col3 |
---|---|---|
123 | ABC | Some Test |
456 | DEF | Another Test |
If I passed the string of Col3
the anticipated outcome would be something like:
Co11 | Col2 | Col3 |
---|---|---|
456 | DEF | Another Test |
123 | ABC | Some Test |
答案1
得分: 1
以下是翻译好的部分:
直接回答问题,您可以像这样对初始列表进行排序:
string colName = "Col3";
rowData.Sort((rd1, rd2) => rd1.First(c => c.Name == colName).Value.CompareTo(rd2.First(c => c.Name == colName).Value));
或者您可以保留原始列表,然后创建一个新的排序列表,像这样:
var sorted = rowData.OrderBy(rd => rd.First(c => c.Name == colName).Value)
.ToList();
根据您的用例,您可能需要考虑使用不同的数据结构来表示您的数据。如果您有很多列,可能会考虑将每一行表示为一个Dictionary,以便快速按名称查找。或者您可以考虑直接使用DataTable类型,尽管这可能有点过于复杂——它有很多方便的功能,但由于试图完成很多任务,所以它比许多其他选项要慢。
英文:
To directly answer the question, you can sort the initial list like this:
string colName = "Col3";
rowData.Sort((rd1, rd2) => rd1.First(c => c.Name == colName).Value.CompareTo(rd2.First(c => c.Name == colName).Value));
Or you can leave the original list alone and created a new sorted list like this:
var sorted = rowData.OrderBy(rd => rd.First(c => c.Name == colName).Value)
.ToList();
Depending on your use case you may want to consider using different data structures to represent your data. If you have a lot of columns, it might make sense to represent each row as a Dictionary for quick lookup by name. Or you might consider using the DataTable type directly, although that might be overkill--it's got a lot of handy features, but it's slower than many other options because it tries to do so many things.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论