0

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.

5
  • Please show what you have already tried and come back with a specific problem Commented Nov 17, 2015 at 11:04
  • 2
    0. Kick the developer who thought it was a good idea to store the data like this. 1. Split the string. 2. Parse the date element. 3. Sort. Commented Nov 17, 2015 at 11:05
  • 1
    Do you want to sort the data on the server, i.e. you want a SQL solution, or in your C# code once you've already retrieved it? Commented Nov 17, 2015 at 11:07
  • Hi,The data is returned from different tables, I am just to output all the data into one area..sorted by date, this is why there are different number of fields. As for splitting and parsing the data by "date" it works for one set...how do i then merge the different sorted arrays. This is the same result as querying different tables..using "ORDER BY" on the sql side Commented Nov 17, 2015 at 11:12
  • I could sort on the server, but again i end up with different number of arrays, is there a way i can combine them all into 1? Commented Nov 17, 2015 at 11:15

2 Answers 2

4

Try something like:

var sorted = input.OrderBy(line => DateTime.Parse(line.Split('|').Last()))
                  .ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

How do i then combine the various "sorted" arrays? or lists? or whatever collection..
Make a list of each array as <string, date>, use list.addrange() to create a single list and then sort on the date
i think Peter has understood my problem! Could you explain the <string, date> please? I don't understand that structure though!
@AbrarAli, you can Concat the arrays and then sort on the Date field: var sorted = arr1.Concat(arr2).OrderBy(line => ...).ToArray();
3

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.

3 Comments

This solves the issue of one array..as does the solution above. I need to solve multiple arrays of different element length, and combine the result into one :)
@AbrarAli I've added another update which I feel a bit happier about. In my opinion it's much easier to read and is easier to maintain and add to.
Heya, I hardly understand the changes you made! im still a learner pretty much :) But thanks though! I do feel the readability was improved though! And thank you I got it to work! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.