locked
How to sort the 2D array according to specific column RRS feed

  • Question

  • I need to know how to sort a 2D array according to a specific column. Thanks in advance

    Dileepa S. Rajapaksa
    ---------------------------------
    Trainee Developer
    Microsoft, Sri Lanka

    ----------------
    Twitter : @dsrajapaksa
    Blog : http://www.dileepatech.net

    Friday, September 12, 2014 4:04 AM

Answers

  • Hi Dileepa S. Rajapaksa ,

    Take a look at Sorting a Two-Dimensional Array in C#

    Firstlly,Using Linq and extension methods , After that simply do something like the following

    int[,] array = new int[3, 3] { { 1, 4, 2 }, { 4, 5, 1 }, { 7, 3, 8 } };
     
    int[,] sortedByFirstElement = array.OrderBy(x => x[0]);
    int[,] sortedBySecondElement = array.OrderBy(x => x[1]);
    int[,] sortedByThirdElement = array.OrderBy(x => x[2]);

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 15, 2014 7:18 AM
  • Hi astrodileepa,

    Here is LNIQ soarting. When you working with collections always there is LINQ/ Lambda option for you. 

    var result = myArray.OrderBy(row => row[columnIndex])

    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    ---------------------------------------

    M Prabath Maduranga Peiris
    Microsoft Student Partner
    Blogs : prabathsl.blogspot.com

    Friday, September 12, 2014 5:12 AM

All replies

  • Hi astrodileepa,

    Here is LNIQ soarting. When you working with collections always there is LINQ/ Lambda option for you. 

    var result = myArray.OrderBy(row => row[columnIndex])

    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    ---------------------------------------

    M Prabath Maduranga Peiris
    Microsoft Student Partner
    Blogs : prabathsl.blogspot.com

    Friday, September 12, 2014 5:12 AM
  • I need to know how to sort a 2D array according to a specific column.

    How do I sort a two-dimensional array in C#?
    http://stackoverflow.com/questions/232395/how-do-i-sort-a-two-dimensional-array-in-c

    "I have a two-dimensional array (of Strings) which make up my data table (of rows and
    columns). I want to sort this array by any column. ..."

    - Wayne

    Friday, September 12, 2014 5:24 AM
  • Hi Dileepa S. Rajapaksa ,

    Take a look at Sorting a Two-Dimensional Array in C#

    Firstlly,Using Linq and extension methods , After that simply do something like the following

    int[,] array = new int[3, 3] { { 1, 4, 2 }, { 4, 5, 1 }, { 7, 3, 8 } };
     
    int[,] sortedByFirstElement = array.OrderBy(x => x[0]);
    int[,] sortedBySecondElement = array.OrderBy(x => x[1]);
    int[,] sortedByThirdElement = array.OrderBy(x => x[2]);

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 15, 2014 7:18 AM
  • OrderBy is not showing intelisense.....is there any library to include for this
    Thursday, November 30, 2017 6:04 AM
  • OrderBy is not showing intelisense.....is there any library to include for this

    Did you look at the article at the link provided by Kristin Xie?

    Here it is again:

    Sorting a Two-Dimensional Array in C#
    https://www.codeproject.com/Tips/166236/Sorting-a-Two-Dimensional-Array-in-Csharp

    - Wayne

    • Edited by WayneAKing Thursday, November 30, 2017 10:12 AM
    Thursday, November 30, 2017 10:02 AM
  • Thank you Kristin for your answer! It worked perfectly!

    Tuesday, May 15, 2018 8:29 PM