Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to sort multiple time and on specific text in c# win. form like i have records in DataTable like this

Record
Id  Name
 1  5% VAT
 2  12.5% CST
 3  12.5% VAT
 4  14% VAT
 5  2% CST



And want Desired Result like this
Id  Name
 1  5% VAT
 2  12.5% VAT
 3  14% VAT
 4  2% CST
 5  12.5% CST


[Note : Database Used : Access (cannot change it to SQL because of client's need)
I want sorting on text :-
1) Sorting on Specific String "VAT" And "CST"
2) Normal Sorting On The String
]


Thanks In Advance

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 9-Mar-14 2:35am
v5
Comments
Kornfeld Eliyahu Peter 9-Mar-14 8:01am    
Did you checked DataTable.DefaultView.Sort?
agent_kruger 9-Mar-14 8:14am    
yes sir, but here i need to sort please read the Note columns in the question.
Kornfeld Eliyahu Peter 9-Mar-14 8:17am    
Let see If I got you... You want sort where values with VAT in it come first and CST after, but in every group you want to sort by value (%)?
agent_kruger 10-Mar-14 8:10am    
sir, you understood the first part right. First I need to sort it with VAT and CST. Second, then with keeping that record in place want to sort the rest in ascending order.
Kornfeld Eliyahu Peter 10-Mar-14 8:27am    
In that case you have to go with OG's advice - pour the content of the DataTable into some sortable (by code) structure, sort it and pour back...

1 solution

You can't sort a DataTable except by using "simple" comparisons on the DataView: there is no built in mechanism for using a custom sort comparer.

So: extract the rows, sort them using Linq or similar, and put the rows back into the DataTable.
That way you can write a comparison method that works any way you need it to.


So...a sample:
C#
    // Set up your sample data
    DataTable dt = new DataTable();
    dt.Columns.Add("Id");
    dt.Columns.Add("Name");
    dt.Rows.Add(1, "5% VAT");
    dt.Rows.Add(2, "12.5% CST");
    dt.Rows.Add(3, "12.5% VAT");
    dt.Rows.Add(4, "14% VAT");
    dt.Rows.Add(5, "2% CST");
    // Extract data to Enumerable
    List<string> list = new List<string>();
    foreach (DataRow row in dt.Rows)
        {
        list.Add((string)row["name"]);
        }
    // Order list
    list = list.OrderBy(s => s, new PercentageComparer()).ToList();
    // Put sorted data back in table
    for (int i = 0; i < dt.Rows.Count; i++)
        {
        dt.Rows[i][1] = list[i];
        }
    // And display
    myDataGridView.DataSource = dt;
    }


public class PercentageComparer : IComparer<string>
    {
    public int Compare(string x, string y)
        {
        string[] partsX = x.Split(' ');
        string[] partsY = y.Split(' ');
        if (partsX[1] == partsY[1])
            {
            // Same subgroup.
            // Return based on percentage
            double dx = double.Parse(partsX[0].Trim('%'));
            double dy = double.Parse(partsY[0].Trim('%'));
            if (dx < dy) return -1;
            if (dx > dy) return 1;
            return 0;
            }
        // Return based on subgroup
        // Note order of compare: VAT before CST...
        return string.Compare(partsY[1], partsX[1]);
        }
    }
 
Share this answer
 
v2
Comments
Kornfeld Eliyahu Peter 9-Mar-14 8:19am    
IMHO - if it's not necessary do not sort data in code, but in DB. He's the best at that.
I would suggest to make some redesign to the data and use pure SQL...
agent_kruger 9-Mar-14 8:22am    
sir, my database in MS ACCESS and according to need of client cannot change it.
Kornfeld Eliyahu Peter 9-Mar-14 8:26am    
Then you have to go and learn LINQ - it can help...
OriginalGriff 9-Mar-14 8:29am    
I'd agree - separate the two items into two columns (one double, one string or FK) and use SQL to load in the order you want - but then you could use the DataView.Sort property anyway! :laugh:
And since he didn't mention a DB in his question, I don't want to assume...
agent_kruger 9-Mar-14 8:33am    
sir, i have already updated the question before you posted the comment (Gotcha!!).

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