Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello I have a dataset consisting of the following values.
Meaning of columns are Line Number, Detail, and color
4	                x3	       White
5	                x4	       White
6	                x5	       Red
6	                x5c	       LightGreen
7	                x6	       Red
7	                x6c	       LightGreen
8	                x7	       White
9	                x7extra	   LightGreen
10	                x8	       White
11	                x9	       Red
11	                x9extra	   LightGreen
12	                x10	       Red
12	                x10extra   LightGreen
13	                x11	       White

I am trying to accomplish the following sorted table.
4	                x3	       White
5	                x4	       White
6	                x5	       Red
7	                x6	       Red
6	                x5c	       LightGreen
7	                x6c	       LightGreen
8	                x7	       White
9	                x7extra	   LightGreen
10	                x8	       White
11	                x9	       Red
12	                x10	       Red
11	                x9extra	   LightGreen
12	                x10extra   LightGreen
13	                x11	       White


First I tried sorting the dataset by columns but couldn't do it. So far what I have tried is first finding the duplicates then finding the consecutive numbers in that duplicates list and somehow make it work.

My code is:
C#
List<int> lstRowNums = new List<int>();
for (int i = 0; i < dt.Rows.Count; i++)
    lstRowNums.Add(Convert.ToInt32(dt.Rows[i]["Line"]));

int[] duplicates = lstRowNums.GroupBy(a => a).SelectMany(ab => ab.Skip(1).Take(1)).ToArray(); 

var sequences = duplicates.GroupBy(num => duplicates.Where(candidate => candidate >= num)
                          .OrderBy(candidate => candidate)
                          .TakeWhile((candidate, index) => candidate == num + index)
                          .Last())
                          .Select(seq => seq.OrderBy(num => num));

But I am stuck here. Can someone help me? Or suggest me different approach how to do this. Thanks in advance
Posted
Updated 23-Apr-15 22:05pm
v4
Comments
Maciej Los 24-Apr-15 8:36am    
I'd suggest to use Extension method or join dataset with corresponding value to each x9, x9extra, etc. Then you'll be able to sort data.

To be able to sort data on non-numeric field and get sorted list in numeric order, you have to do some trick. You need a dictionary[^], where TKey should be Detail and TValue should be corresponding numeric value. That's all!

C#
Dictionary<string, int> DetailsDict = new Dictionary<string, int>();
DetailsDict.Add("x3", 3);
DetailsDict.Add("x4", 4);
//...
DetailsDict.Add("x10extra", 15);
DetailsDict.Add("x11", 16);


Usage:
C#
var result = ...
        .OrderBy(a=>DetailsDict[a.Detail]);



Second solution



Another way is to write extension method as follow:
C#
static int GetMyNumber(string test)
{
    int retVal = 0;
    Int32.TryParse(test.Replace("c","").Replace("extra","").Replace("x",""), out retVal);
    return retVal;
}


Usage:
XML
var result = ...
            .Select(a=>new
            {
                origVal = a.Detail,
                newVal = GetMyNumber(a.Detail)
            }
            )
            .OrderBy(a=>a.newVal).ThenBy(a=>a.origVal);
 
Share this answer
 
Comments
Sascha Lefèvre 27-Apr-15 10:57am    
Math.Ceiling(2.236*2.236) !
Maciej Los 27-Apr-15 11:24am    
;)
Why not just use OrderBy on color, and ThenBy[^] on Detail?
 
Share this answer
 
Comments
wonder-FOOL 24-Apr-15 3:58am    
Ordering by color will change the line number order. I think I should somehow group by the duplicate row numbers and then order it or manupilate.
As a second information I wrote the detail part to make my question to be more understandable.

For example; Let's say: On the detail part line number 5; Detail data can be a and b. So ordering on the detail column will not make any sence.
But no matter what on the duplicate rows color code red always comes first then the color LightGreen.

Sorry for not being clear on the question. My basic need is;
- Line number is important it has to be grouped by color. (Saying it seems easy but I couldn't do it)
Maciej Los 24-Apr-15 4:13am    
Have you tested it? It won't help... ;(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900