C# Sortable collection which allows duplicate keys

前端 未结 16 2132
旧巷少年郎
旧巷少年郎 2020-11-28 10:06

I am writing a program to set a sequence in which various objects will appear in report. The sequence is the Y position (cell) on Excel spreadsheet.

A demo part of co

16条回答
  •  执念已碎
    2020-11-28 10:58

    Create a class and query the list:

    Public Class SortingAlgorithm
    {
        public int ID {get; set;}
        public string name {get; set;}
        public string address1 {get; set;}
        public string city {get; set;}
        public string state {get; set;}
        public int age {get; set;}
    }
    
    //declare a sorting algorithm list
    List sortAlg = new List();
    
    //Add multiple values to the list
    sortAlg.Add( new SortingAlgorithm() {ID = ID, name = name, address1 = address1, city = city, state = state, age = age});
    sortAlg.Add( new SortingAlgorithm() {ID = ID, name = name, address1 = address1, city = city, state = state, age = age});
    sortAlg.Add( new SortingAlgorithm() {ID = ID, name = name, address1 = address1, city = city, state = state, age = age});
    
    //query and order by the list
      var sortedlist = (from s in sortAlg
                        select new { s.ID, s.name, s.address1, s.city, s.state, s.age })
                                                         .OrderBy(r => r.ID)
                                                         .ThenBy(r=> r.name)
                                                         .ThenBy(r=> r.city)
                                                         .ThenBy(r=>r.state)
                                                         .ThenBy(r=>r.age);
    

提交回复
热议问题