Hi,
I faced a question in interview..
Q: Suppose I have a table like this:
Name | E1 | E2
------------|-------|------------------------
A | 9 | 6
B | 5 | 7
C | 8 | 9
D | 5 | 9
E | 9 | 9
Arrange 'Name' sorted by assending order of 'E1'.
Name | E1 | E2
---------|-----|------------------------
A | 9 | 6
E | 9 | 9
C | 8 | 9
B | 5 | 7
D | 5 | 9
then if E1 is same so sorted by E2.
Name | E1 | E2
---------|-----|------------------------
E | 9 | 9
A | 9 | 6
C | 8 | 9
D | 5 | 9
B | 5 | 7
So how can we do this with array?
11 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Upendra Pratap ShahiPosted Aug 20, 2015, 2:39 AM
Hello use below code-
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
public partial class TestPage2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
sortArr();
}
}
public void sortArr()
{
User[] users = new User[4] { new User("Upendra", 5, 5000), new User("Ashish", 3, 7000), new User("Gitendra", 1, 9000), new User("Surender", 5, 6000) };
Response.Write("USERS Record
");
Response.Write("
");
for (int i = 0; i < 4; i++)
{
Response.Write(users[i].Name+" ");
Response.Write(users[i].GradePoint+ " ");
Response.Write(users[i].Salary+" ");
Response.Write("
");
}
//sort array by name, by grade, by salary
users = users.OrderByDescending(x => x.Name).ThenByDescending(x => x.GradePoint).ThenByDescending(x => x.Salary).ToArray();
Response.Write("
");
Response.Write("USERs Record
");
Response.Write("
");
for (int i = 0; i < 4; i++)
{
Response.Write(users[i].Name+" ");
Response.Write( users[i].GradePoint+" ");
Response.Write( users[i].Salary+" ");
Response.Write("
");
}
}
}
class User
{
public string Name { get; set; }
public int GradePoint { get; set; }
public int Salary { get; set; }
//constructor
public User(string name, int gradepoint, int sal)
{
this.Name = name;
this.GradePoint = gradepoint;
this.Salary = sal;
}
}
Kindly mark as accepted answer.Upendra Pratap ShahiPosted Aug 20, 2015, 12:14 PM
Ankit ShuklaPosted Aug 20, 2015, 9:32 AM
Ankit ShuklaPosted Aug 20, 2015, 9:09 AM
D 5 9
C 8 9
B 5 7
A 9 6
Upendra Pratap ShahiPosted Aug 20, 2015, 8:36 AM
Ankit ShuklaPosted Aug 20, 2015, 8:32 AM
Rajeesh MenothPosted Aug 20, 2015, 1:56 AM
Saroj Kumar SahuPosted Aug 20, 2015, 1:55 AM
Karthikeyan KPosted Aug 20, 2015, 1:29 AM
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> CREATE TABLE TABLE1(NAME VARCHAR(1),V1 NUMBER,V2 NUMBER);
Table created.
SQL> INSERT INTO TABLE1(NAME,V1,V2) VALUES('&NAME','&V1','&V2');
Enter value for name: K
Enter value for v1: 78
Enter value for v2: 99
old 1: INSERT INTO TABLE1(NAME,V1,V2) VALUES('&NAME','&V1','&V2')
new 1: INSERT INTO TABLE1(NAME,V1,V2) VALUES('K','78','99')
1 row created.
SQL> /
Enter value for name: A
Enter value for v1: 99
Enter value for v2: 99
old 1: INSERT INTO TABLE1(NAME,V1,V2) VALUES('&NAME','&V1','&V2')
new 1: INSERT INTO TABLE1(NAME,V1,V2) VALUES('A','99','99')
1 row created.
SQL> SELECT * FROM TABLE1;
N V1 V2
- ---------- ----------
K 78 99
A 99 99
SQL> SELECT * FROM TABLE1 ORDER BY V1 DESC, V2 DESC;
N V1 V2
- ---------- ----------
A 99 99
K 78 99
Ankit ShuklaPosted Aug 20, 2015, 1:27 AM
Rahul PrajapatPosted Aug 20, 2015, 1:05 AM