Hassan
Hassan

Reputation: 5430

DataTable Column value grouping and sorting

ColumnA of DataTable contains 3 type of content:

  1. Numeric
  2. Alphanumeric
  3. Alphabets

ColumnB of DataTable contains date format "YYYYMMDD".

I need data to be grouped based on 3 content types and then sort by date ascending order.

Example:

ColumnA | ColumnB
A123457 | 20171114 
B246734 | 20171009
1234544 | 20170808
6789033 | 20171220
ABBCDEE | 20180102
A112233 | 20160202
1212122 | 20171115
NNNNNNN | 20171011

Grouping based on ColumnA Value and Sorting based on ColumnB value:

ColumnA | ColumnB
1234544 | 20170808
1212122 | 20171115
6789033 | 20171220

A112233 | 20160202
B246734 | 20171009
A123457 | 20171114

NNNNNNN | 20171011
ABBCDEE | 20180102

What we will be LINQ solution for this?

Upvotes: 2

Views: 119

Answers (1)

Chidambaram
Chidambaram

Reputation: 444

Below is not the exact code, by the below way you can achieve the expected result.

//sort the date and put into same list
CultureInfo provider = CultureInfo.InvariantCulture;
                    myList.ForEach(i => { i.ColumnB = DateTime.ParseExact(i.ColumnB, "YYYYMMDD", provider);});

//check for regex match and add to list
                    mynumericlist = myList.Where(x => Regex.IsMatch(x.columnA, @"^\d$")).ToList().OrderBy(x=>x.ColumnB);
                    myalphalist = myList.Where(x => Regex.IsMatch(x.columnA, @"^[a-zA-Z]*$")).ToList().OrderBy(x => x.ColumnB);
                    myalpnumlist = myList.Where(x => Regex.IsMatch(x.columnA, @"^[a-zA-Z0-9]*$")).ToList().OrderBy(x => x.ColumnB);

                    //then combine all

Upvotes: 2

Related Questions