How can i sort Generic list with Linq?
Asked Answered
L

5

4

How can i sort myScriptCellsCount.MyCellsCharactersCount (list int type) in linq

    public class MyExcelSheetsCells
    {
        public List<int> MyCellsCharactersCount { get; set; }

        public MyExcelSheetsCells()
        {
            MyCellsCharactersCount = new List<int>();
        }

    }
   void ArrangedDataList(DataTable dTable)
        {
            DAL.MyExcelSheets myexcelSheet = new DAL.MyExcelSheets();
            myScriptCellsCount = new TestExceltoSql.DAL.MyExcelSheetsCells();

            foreach (DataColumn col in dTable.Columns)
                myexcelSheet.MyColumnNames.Add(col.ColumnName.ToString());
            foreach(DataColumn dc in dTable.Columns)
            foreach (DataRow  dr in dTable.Rows)
                myScriptCellsCount.MyCellsCharactersCount.Add(dr[dc].ToString().Length);
          //How can i sort desc
            //myScriptCellsCount.MyCellsCharactersCount = from list in myScriptCellsCount.MyCellsCharactersCount
            //                                            orderby list.CompareTo( descending
            //                                            select list;
            CreatSqlTable(myexcelSheet.MyColumnNames, dTable.TableName, myScriptCellsCount.MyCellsCharactersCount[0].ToString());
            myscript.WriteScript(myscript.SqlScripts);
        }
Liv answered 17/6, 2010 at 14:10 Comment(0)
S
9
// using Linq
MyCellsCharactersCount.OrderBy(x => x);            // ascending
MyCellsCharactersCount.OrderByDescending(x => x);  // descending

or

// not using Linq
MyCellsCharactersCount.Sort();                     // ascending
MyCellsCharactersCount.Sort().Reverse();           // descending
Servo answered 17/6, 2010 at 14:12 Comment(1)
It should be OrderByDescending.Diligence
O
9

You can use OrderBy or Sort, but there is a difference between the 2 that you should understand:

If you do sort, it sorts your list "in place", so in this example, the variable "list" gets sorted:


// you can manipulate whether you return 1 or -1 to do ascending/descending sorts
list.Sort((x, y) =>
{
   if (x > y) return 1;
   else if (x == y) return 0;
   else return -1;
});

If you do an OrderBy, the original list is unaffected, but a new, sorted enumerable is returned:

var sorted = list.OrderByDescending(x => x)

Edit

This answer was recently upvoted, so I reviewed it. In my original response, I left out a really important detail:

If you use the LINQ code above (second example), the sort is going to occur every time you iterate over the variable "sorted". So, if you have it in more than 1 foreach, you will repeat the sort. To avoid that, change the code above to:

var sorted = list.OrderByDescending(x => x).ToList(); // or .ToArray()

That will force the enumerator to run, and will store the result in sorted.

If you are only going to enumerate it once, you can leave out the ToList/ToArray call.

Oatmeal answered 17/6, 2010 at 14:24 Comment(0)
D
1

You should be able to use the OrderBy method on your list.

IEnumerable sortedList = myScriptCellsCount.MyCellsCharactersCount.OrderBy(anInt => anInt);
Dimetric answered 17/6, 2010 at 14:13 Comment(0)
H
0
var sorted = from i in MyCellsCharacterCount orderby i descending select i;
Hughes answered 8/8, 2012 at 10:2 Comment(0)
E
0

Take a look at this answer. You can replace "Process" with your object by getting typeOf your List.

Cheers

Exordium answered 16/10, 2012 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.