Search code examples
c#.netasp.net-mvc.net-coreasp.net-core-mvc

How to sort a list with two columns in ASP.NET Core MVC using Razor?


var xx = _context.tablename.OrderBy(s=> new { s.ordering, s.data_id }).ToList();
var xx = _context.tablename.OrderBy(s=> s.ordering).ThenBy(s=> s.data_id).ToList();

Both conditions not giving desired result

My list

A B
1 1
2 1
3 2
5 2
6 3
4 3

Required List:

A B
1 1
2 2
3 3
4 1
5 2
6 3

What is the best way to do it?


Solution

  • According to your list and code, your two columns of data should be Id and ordering. But the final result you want disrupts the corresponding order of the database, so you need to process it separately after fetching the data.

    I made a simple example, you can refer to it.

    Model:

    public class TableModel
    {
        [Key]
        public int data_id { get; set; }
        public int ordering { get; set; }
    }
    

    Controller:

    public IActionResult Index()
    {
        //sort 
        var xx1 = _context.TableModel.OrderBy(s => s.data_id).ToList();
        var aa1 = new List<int>();
        foreach (var item in xx1)
        { 
            aa1.Add(item.data_id);
        }
    
        //sort ordering
        var xx2 = _context.TableModel.OrderBy(s => s.ordering).ToList();
        var aa2 = new List<int>();
        foreach (var item in xx2)
        { 
            aa2.Add(item.ordering);
        }
        var aa2Fin = new List<int>();
        //get min number
        var min = aa2.Min();
        //add a min number
        aa2Fin.Add(min);
        //add the first group
        foreach (var item in aa2)
        {
            if (item >= min && !aa2Fin.Contains(item))
            { 
                aa2Fin.Add(item);
            }
        }
    
        //remove the first group
        foreach(var item in aa2Fin)
        {
            aa2.Remove(item);
        }
    
        //add the second group
        foreach (var item in aa2)
        {
            aa2Fin.Add(item);
        }
    
        List<TableModel> tableModels= new List<TableModel>(); 
        for (var i = 0; i < aa1.Count(); i++)
        {
            tableModels.Add(new TableModel() { data_id = aa1[i], ordering = aa2Fin[i] });
        }
        return View(tableModels);
    }
    

    View:

    @model List<TestApp.Models.TableModel>
    
    <table class="table">
        <thead>
            <tr>
                <td>A</td>
                <td>B</td>
            </tr>
        </thead>
        <tbody>
            @foreach(var item in Model)
            {
                <tr>
                    <td>@item.data_id</td>
                    <td>@item.ordering</td>
                </tr>
            }
        </tbody>
    </table>
    

    Test Result: enter image description here

    Of course, I'm just building a test with only two duplicate values based on what you proposed. If there are more repetitions in a set of data, such as three 1... then you need more rigorous logic. For example, integrate the data into an array [{1,2,3,4},{1,2,3},{1,2}...], and then merge.