How to sort a string array which includes a date in c#?

江枫思渺然 提交于 2020-01-07 09:02:46

问题


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 is a part of each element. I am reading back the information from sql server table.

How could I go about doing something like this?

An example of the data could be something like this:

"TYPE|Field1|Field2|Date"

"TYPE|Field1|Field2|Date"

"TYPE|Field1|Field2|Date"

And for the second array:

"TYPE|Field1|Field2|Field3|Date"

"TYPE|Field1|Field2|Field3|Date"

"TYPE|Field1|Field2|Field3|Date"

etc

And the same will apply to the other string arrays.

So essentially, how to sort and merge multiple string arrays based on the date?

I could easily go to a list if that helps solve the issue.

Oh, The other issue is the different arrays wont have the same number of data inside it.


回答1:


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

Update

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

Update 2

A more object orientated approach

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.




回答2:


Try something like:

var sorted = input.OrderBy(line => DateTime.Parse(line.Split('|').Last()))
                  .ToArray();


来源:https://stackoverflow.com/questions/33755287/how-to-sort-a-string-array-which-includes-a-date-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!