I\'m not sure if this has been asked before, but I have few string array which I need to sort. The idea is to merge different string arrays and sort them by the date field which
Here is a working example of your code. It will sort the array no matter how many fields it has. You may want to change the change the format depending on what you are reading in.
var arr1 = new string[] {
"TYPE|Field1|Field2|2015-01-03 00:00:00",
"TYPE|Field1|Field2|2014-01-07 00:00:00",
"TYPE|Field1|Field2|2015-01-04 00:00:00"
};
var orderedArr1 = arr1.OrderBy(a => DateTime.ParseExact(a.Substring(a.LastIndexOf('|') + 1), "yyyy-MM-dd hh:mm:ss", null ));
foreach(var a in orderedArr1)
{
Console.WriteLine(a);
}
Merge 2 arrays then do the sorting:
var arr1 = new string[] {
"TYPE|Field1|Field2|2015-01-03 00:00:00",
"TYPE|Field1|Field2|2014-01-07 00:00:00",
"TYPE|Field1|Field2|2015-01-04 00:00:00"
};
var arr2 = new string[] {
"TYPE|Field1|Field2|Field3|2015-01-04 00:00:00",
"TYPE|Field1|Field2|Field3|2014-01-02 00:00:00",
"TYPE|Field1|Field2|Field3|2015-01-06 00:00:00"
};
var mergedArrs = arr1.Concat(arr2);
var orderedArr1 = mergedArrs.OrderBy(a => DateTime.ParseExact(a.Substring(a.LastIndexOf('|') + 1), "yyyy-MM-dd hh:mm:ss", null));
foreach(var a in orderedArr1)
{
Console.WriteLine(a);
}
The above code was bothering me, so I thought I would refactor it in to something a bit more readable and easier to maintain.
class Program
{
static void Main(string[] args)
{
var arr1 = new string[] {
"TYPE|Field1|Field2|2015-01-03 00:00:00",
"TYPE|Field1|Field2|2014-01-07 00:00:00",
"TYPE|Field1|Field2|2015-01-04 00:00:00"
};
var arr2 = new string[] {
"TYPE|Field1|Field2|Field3|2015-01-04 00:00:00",
"TYPE|Field1|Field2|Field3|2014-01-02 00:00:00",
"TYPE|Field1|Field2|Field3|2015-01-06 00:00:00"
};
var mergedData = new List<TableData>();
mergedData.Append(arr1);
mergedData.Append(arr2);
foreach (var item in mergedData.OrderBy(a => a.Date))
{
Console.WriteLine(item.RawData);
}
}
}
public struct TableData
{
public DateTime Date { get; set; }
public string RawData { get; set; }
}
public static class Extensions
{
public static void Append(this List<TableData> list, string[] items)
{
foreach(var item in items)
{
var date = DateTime.ParseExact(item.Substring(item.LastIndexOf('|') + 1), "yyyy-MM-dd hh:mm:ss", null);
list.Add(new TableData { Date = date, RawData = item });
}
}
}
Essentially we're creating a list of TableData
(name it as you please) that has a DateTime Date
and string RawData
field that we can extract the data to. We have an extension method Append
for a List of type TableData
that accepts a string[]
. The method iterates over each string in the string array, pulls out the date using the same method from before, creates a TableData
object with the data & raw data, and adds it to our list.
You can do this with as many string[]
as you like. Once you have added all of the arrays we're good to go in to our for
loop. The ordering of our list is done here; and personally I think that's much more readable. Feel free to change the names of things and fit it in to your application as you need.
Try something like:
var sorted = input.OrderBy(line => DateTime.Parse(line.Split('|').Last()))
.ToArray();