-4
| ID         | Desc        | Priority     |
|:-----------|------------:|:------------:|
| 1          |        aabc |   Priority1     
| 2          |         xyz |   Priority3    
| 3          |         cba |   Priority5     
| 4          |          fg |   Priority2      
| 5          |         xdr |   Priority4    
| 6          |         pqr |   Priority1

Hi, I've above table stored in a form of an Array.

Array[] Values;


Values[0]={'ID','Desc','Priority'};
Values[1]={'1','aabc','Priority1'};
Values[2]={'2','xyz','Priority3'};
Values[3]={'3','cba','Priority5'};

Now I have to sort Values on the basis of priority so that my result comes as.

Values[0]={'ID','Desc','Priority'};
Values[1]={'1','aabc','Priority1'};
Values[2]={'4','fg','Priority2'};
Values[3]={'2','xyz','Priority3'};

help me write the code for this in C#

5
  • What is Array type. Any Definition ? Commented Jan 14, 2016 at 11:57
  • 1
    Possible duplicate of Better way to sort array in descending order Commented Jan 14, 2016 at 12:02
  • This is literally one line in LINQ. Have you tried anything? Commented Jan 14, 2016 at 12:02
  • msdn.microsoft.com/en-us/library/bb383982.aspx , as @InBetween says it really not a good idea to have priority as string Commented Jan 14, 2016 at 12:13
  • Create a class for your table. dont use arrays like that. because there is no type safety or any information of how you represent your data. this makes your program hard to understand and can become real buggy. Commented Jan 14, 2016 at 12:18

2 Answers 2

1
using System.Linq;

Values.OrderBy(a => a[2]);

Without more context can't really say for sure but Values looks horrendous. Why not use, for instance, an int to define priorities?

Sign up to request clarification or add additional context in comments.

Comments

0

If you really have a DataTable then try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication66
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Desc", typeof(string));
            dt.Columns.Add("Priority", typeof(string));

            dt.Rows.Add(new object[] {1, "aabc", "Priority1"});     
            dt.Rows.Add(new object[] {2, "xyz", "Priority3"});     
            dt.Rows.Add(new object[] {3, "cba", "Priority5"});     
            dt.Rows.Add(new object[] {4, "fg", "Priority2"});     
            dt.Rows.Add(new object[] {5, "xdr", "Priority4"});     
            dt.Rows.Add(new object[] {6, "pqr", "Priority1"});

            DataTable results = dt.AsEnumerable()
                .OrderBy(x => x.Field<string>("Priority"))
                .CopyToDataTable();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.