I am reading a .csv file and returning its lines in string array. One of the members is manufacturer, for which I have Toyota, Ford, etc.
我正在读一个.csv文件,并在字符串数组中返回它的行。其中一个成员是制造商,我有丰田,福特等。
I want to sort an array (Can be another collection) of the rows, by the value in manufacturer and alphabetical order.
我想按行制造商的数字和字母顺序对行的数组(可以是另一个集合)进行排序。
So I'd have:
所以我有:
28437 Ford Fiesta
328 Honda Civic
34949 Toyota Yaris
and so forth...
等等......
What would be the best way to do this using C# and no database? I say no database because I could insert the csv into a table in a sql server database, and then query it and return the data. But this data is going into a html table built on the fly, which would make the database approach a little long winded.
使用C#和没有数据库的最佳方法是什么?我说没有数据库,因为我可以将csv插入到sql server数据库的表中,然后查询它并返回数据。但是这些数据将进入一个动态构建的html表,这将使数据库方法有点长。
4 个解决方案
#1
2
What version of .NET are you using? If you're using 3.5 - or can use C# 3.0 and LINQBridge - then I'd definitely go with LINQ. First transform each line into some appropriate object, and then use OrderBy:
您使用的是什么版本的.NET?如果您使用3.5 - 或者可以使用C#3.0和LINQBridge - 那么我肯定会使用LINQ。首先将每一行转换为一些适当的对象,然后使用OrderBy:
var cars = lines.Select(line => Car.ParseLine(line))
.OrderBy(car => car.Manufacturer);
#2
1
Without trying to second guess Jon, I believe he is suggesting that you would create a class Car ("some appropriate object") with the necessary properties, and the ability to populate a Car from a line:
在没有试图再次猜测Jon的情况下,我相信他建议你创建一个具有必要属性的类Car(“一些适当的对象”),以及从一行填充Car的能力:
public class Car
{
public int Id {get;set;}
public string Manufacturer {get;set;}
public string Model {get;set;}
public static Car ParseLine(string line)
{
string[] parts = line.Split(DELIMITER);
return new Car
{
Id = int.Parse(parts[0]),
Manufacturer = parts[1],
Model = parts[2]
};
}
}
i.e. treating the lines as objects. Then with LINQ things become quite simple:
即将线条视为物体。然后使用LINQ,事情变得非常简单:
var query = from line in lines
let car = Car.ParseLine(line)
orderby car.Manufacturer
select car;
var arr = query.ToArray();
Note that you can do this without LINQ too, for example (using a Car[]
array) - an in-place array sort:
请注意,您也可以在没有LINQ的情况下执行此操作,例如(使用Car []数组) - 就地数组排序:
Array.Sort(arr, (x, y) => string.Compare(x.Manufacturer, y.Manufacturer));
or the same with List<Car>
:
或与List
list.Sort((x, y) => string.Compare(x.Manufacturer, y.Manufacturer));
#3
1
In Jon's post he is parsing the line into objects first, then sorting. You could also just sort an IEnumerable list of strings based on part of the string. Here is an example that is sorting the list on a substring (first 10 characters starting at position 6):
在Jon的帖子中,他首先将行解析为对象,然后进行排序。您还可以根据字符串的一部分对IEnumerable字符串列表进行排序。这是一个在子字符串上排序列表的示例(从位置6开始的前10个字符):
List<string> lines = new List<string>
{
"34949 Toyota Yaris",
"328 Honda Civic",
"28437 Ford Fiesta"
};
var sortedLines = lines.OrderBy(line => line.Substring(6, 10));
// Or make it case insensitive
// var sortedLines = lines.OrderBy(line => line.Substring(6, 10), StringComparer.InvariantCultureIgnoreCase);
foreach (var line in sortedLines)
{
Console.WriteLine(line);
}
#4
1
If you just have a bunch of strings in an Array, use
如果你在数组中只有一堆字符串,请使用
Array.Sort(myArray)
That will put the strings in "myArray" in alphabetical order (case sensitive).
这会将字符串按字母顺序排列在“myArray”中(区分大小写)。
If you want to to different comparisons (like case-insensitive for example), you can define an own ICcomparer or use Linq-Extensions, preferrably with a lambda expression like
如果你想进行不同的比较(例如不区分大小写),你可以定义一个自己的ICcomparer或使用Linq-Extensions,最好使用lambda表达式
string [] sArray = new string[] { "fsdhj", "FA", "FX", "fxx", "Äbc" };
sArray = sArray.OrderBy(s => s.ToLowerInvariant()).ToArray();
There's a whole bunch of other sorting methods, but these are the most basic. I could give you a more detailed answer to your problem, if I understood better what your input-object looks like. As long as it's just an array of strings, you should be fine with the above.
还有很多其他的排序方法,但这些是最基本的。如果我更好地理解你的输入对象是什么样的,我可以给你一个更详细的问题答案。只要它只是一个字符串数组,你应该没有上述的。
In response to the first two comments below I should also note that the invariant string-sorting method given above is not the best one for that particular job (see comments).
在回答下面的前两条评论时,我还应该注意到上面给出的不变字符串排序方法对于那个特定的工作来说不是最好的(参见注释)。
However it does illustrate the use of extension methods with lambda expressions, which come in very handy in situations where you don't have predefined IComparer-classes.
但是,它确实说明了使用带有lambda表达式的扩展方法,在没有预定义IComparer类的情况下,它们非常方便。
#1
2
What version of .NET are you using? If you're using 3.5 - or can use C# 3.0 and LINQBridge - then I'd definitely go with LINQ. First transform each line into some appropriate object, and then use OrderBy:
您使用的是什么版本的.NET?如果您使用3.5 - 或者可以使用C#3.0和LINQBridge - 那么我肯定会使用LINQ。首先将每一行转换为一些适当的对象,然后使用OrderBy:
var cars = lines.Select(line => Car.ParseLine(line))
.OrderBy(car => car.Manufacturer);
#2
1
Without trying to second guess Jon, I believe he is suggesting that you would create a class Car ("some appropriate object") with the necessary properties, and the ability to populate a Car from a line:
在没有试图再次猜测Jon的情况下,我相信他建议你创建一个具有必要属性的类Car(“一些适当的对象”),以及从一行填充Car的能力:
public class Car
{
public int Id {get;set;}
public string Manufacturer {get;set;}
public string Model {get;set;}
public static Car ParseLine(string line)
{
string[] parts = line.Split(DELIMITER);
return new Car
{
Id = int.Parse(parts[0]),
Manufacturer = parts[1],
Model = parts[2]
};
}
}
i.e. treating the lines as objects. Then with LINQ things become quite simple:
即将线条视为物体。然后使用LINQ,事情变得非常简单:
var query = from line in lines
let car = Car.ParseLine(line)
orderby car.Manufacturer
select car;
var arr = query.ToArray();
Note that you can do this without LINQ too, for example (using a Car[]
array) - an in-place array sort:
请注意,您也可以在没有LINQ的情况下执行此操作,例如(使用Car []数组) - 就地数组排序:
Array.Sort(arr, (x, y) => string.Compare(x.Manufacturer, y.Manufacturer));
or the same with List<Car>
:
或与List
list.Sort((x, y) => string.Compare(x.Manufacturer, y.Manufacturer));
#3
1
In Jon's post he is parsing the line into objects first, then sorting. You could also just sort an IEnumerable list of strings based on part of the string. Here is an example that is sorting the list on a substring (first 10 characters starting at position 6):
在Jon的帖子中,他首先将行解析为对象,然后进行排序。您还可以根据字符串的一部分对IEnumerable字符串列表进行排序。这是一个在子字符串上排序列表的示例(从位置6开始的前10个字符):
List<string> lines = new List<string>
{
"34949 Toyota Yaris",
"328 Honda Civic",
"28437 Ford Fiesta"
};
var sortedLines = lines.OrderBy(line => line.Substring(6, 10));
// Or make it case insensitive
// var sortedLines = lines.OrderBy(line => line.Substring(6, 10), StringComparer.InvariantCultureIgnoreCase);
foreach (var line in sortedLines)
{
Console.WriteLine(line);
}
#4
1
If you just have a bunch of strings in an Array, use
如果你在数组中只有一堆字符串,请使用
Array.Sort(myArray)
That will put the strings in "myArray" in alphabetical order (case sensitive).
这会将字符串按字母顺序排列在“myArray”中(区分大小写)。
If you want to to different comparisons (like case-insensitive for example), you can define an own ICcomparer or use Linq-Extensions, preferrably with a lambda expression like
如果你想进行不同的比较(例如不区分大小写),你可以定义一个自己的ICcomparer或使用Linq-Extensions,最好使用lambda表达式
string [] sArray = new string[] { "fsdhj", "FA", "FX", "fxx", "Äbc" };
sArray = sArray.OrderBy(s => s.ToLowerInvariant()).ToArray();
There's a whole bunch of other sorting methods, but these are the most basic. I could give you a more detailed answer to your problem, if I understood better what your input-object looks like. As long as it's just an array of strings, you should be fine with the above.
还有很多其他的排序方法,但这些是最基本的。如果我更好地理解你的输入对象是什么样的,我可以给你一个更详细的问题答案。只要它只是一个字符串数组,你应该没有上述的。
In response to the first two comments below I should also note that the invariant string-sorting method given above is not the best one for that particular job (see comments).
在回答下面的前两条评论时,我还应该注意到上面给出的不变字符串排序方法对于那个特定的工作来说不是最好的(参见注释)。
However it does illustrate the use of extension methods with lambda expressions, which come in very handy in situations where you don't have predefined IComparer-classes.
但是,它确实说明了使用带有lambda表达式的扩展方法,在没有预定义IComparer类的情况下,它们非常方便。