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?

Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Aug 19, 2015, 10:39 PM EDT
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.Ankit ShuklaRank #2,120 · 672 reputationPosted Aug 20, 2015, 5:32 AM EDT
Rahul PrajapatRank #1,209 · 1.5K reputationPosted Aug 19, 2015, 9:05 PM EDT
Ankit ShuklaRank #2,120 · 672 reputationPosted Aug 19, 2015, 9:27 PM EDT
Karthikeyan KRank #554 · 4.4K reputationPosted Aug 19, 2015, 9:29 PM EDT
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
Saroj Kumar SahuRank #1,766 · 849 reputationPosted Aug 19, 2015, 9:55 PM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Aug 19, 2015, 9:56 PM EDT
Ankit ShuklaRank #2,120 · 672 reputationPosted Aug 20, 2015, 4:32 AM EDT
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Aug 20, 2015, 4:36 AM EDT
Ankit ShuklaRank #2,120 · 672 reputationPosted Aug 20, 2015, 5:09 AM EDT
D 5 9
C 8 9
B 5 7
A 9 6
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted Aug 20, 2015, 8:14 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts