how can I sort rows of a 2D array in ascending order?

Recommended Answers

All 5 Replies

Hi, check here for solution.

Here is a code example:

public class ArrayComparer : System.Collections.IComparer
{
int ix;
public ArrayComparer(int SortFieldIndex)
{
ix = SortFieldIndex;
}

public int Compare(object x, object y)
{
IComparable cx = (IComparable)((Array)x).GetValue(ix);
IComparable cy = (IComparable)((Array)y).GetValue(ix);
return cx.CompareTo(cy);
}
}


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

string[][] lines = new string[4][];
lines[0] = new string[] {"1","a","d"};
lines[1] = new string[] {"2","b","c"};
lines[2] = new string[] {"3","c","b"};
lines[3] = new string[] {"4","d","a"};

foreach (string[] line in lines)
{
Console.WriteLine(line[0]);
}
System.Array.Sort(lines,new ArrayComparer(2));
foreach (string[] line in lines)
{
Console.WriteLine(line[0]);
}

}
string[][] lines = new string[4][];
            lines[0] = new string[] { "1", "a", "d" };
            lines[1] = new string[] { "2", "b", "c" };
            lines[2] = new string[] { "3", "c", "b" };
            lines[3] = new string[] { "4", "d", "a" };
            MakeDescending(lines[0]);
            foreach (string s in lines[0])
           {
               MessageBox.Show(s);
           }
public Array MakeAssending(Array data)
        {
            Array.Sort(data);
            return data;
        }

        public Array MakeDescending(Array data)
        {
            Array.Reverse(data);
            return data;
        }

My Console Application window can't display letters from other alphabets(e.g russian) when I run the program. It's just displaying question marks in place of them. Please help if you can.

...or...

using System;
using System.Linq;

namespace DW_370908
{
   class CDW_370908
   {
      static void Main(string[] args)
      {
         int[][] int_arr_arr = new int[][]
         {
            new int[] {2,190},
            new int[] {7,101},
            new int[] {1,100},
            new int[] {3,100},
            new int[] {9,100}
         };

         int[][] int_arr_arr2 = int_arr_arr.OrderBy(i => i[0].ToString()).ToArray();
         int[][] int_arr_arr3 = int_arr_arr.OrderByDescending(i => i[0].ToString()).ToArray();
         
         int_arr_arr2.ToList().ForEach(i => Console.WriteLine("{0} {1}", i[0], i[1]));
         int_arr_arr3.ToList().ForEach(i => Console.WriteLine("{0} {1}", i[0], i[1]));
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.