Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c82d8a9

Browse files
Create SortTest.java
1 parent 18e4c47 commit c82d8a9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎Collections/SortTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//version 11.1
2+
3+
//use of Comparable Interface
4+
5+
import java.util.*;
6+
class Student implements Comparable
7+
{
8+
int rollno,marks;
9+
String name;
10+
Student(String name ,int rollno , int marks)
11+
{
12+
this.name=name;
13+
this.rollno=rollno;
14+
this.marks=marks;
15+
}
16+
public String toString()
17+
{
18+
return(name+" "+rollno+" "+marks);
19+
}
20+
public int compareTo(Object ob)
21+
{
22+
Student st1=(Student)ob;
23+
return(rollno - st1.rollno);
24+
}
25+
}
26+
class SortTest
27+
{
28+
public static void main(String s[])
29+
{
30+
ArrayList al=new ArrayList();
31+
al.add(new Student("abc",103,86));
32+
al.add(new Student("xyz",101,45));
33+
al.add(new Student("abc",104,95));
34+
al.add(new Student("hij",102,75));
35+
36+
Collections.sort(al);
37+
38+
List l=Collections.synchronizedList(al);
39+
40+
Iterator itr=al.iterator();
41+
while(itr.hasNext())
42+
{
43+
System.out.println(itr.next()); }
44+
}
45+
}
46+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /