-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectSort.java
More file actions
39 lines (32 loc) · 835 Bytes
/
ObjectSort.java
File metadata and controls
39 lines (32 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import javax.print.DocFlavor;
import java.util.*;
public class ObjectSort {
public static void main(String[] args) {
Student [] arr = new Student[4];
arr[0] = new Student("aa",80);
arr[1] = new Student("bb",90);
arr[2] = new Student("cc",60);
arr[3] = new Student("dd",85);
Arrays.sort(arr);
for (Student s : arr){
System.out.println(s.name);
}
}
}
class Student implements Comparable<Student> {
String name;
int marks;
public Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
@Override
public int compareTo(Student o) {
if (this.marks > o.marks){
return 1;
} else if (this.marks < o.marks) {
return -1;
}
return 0;
}
}