Repository navigation
Expand file tree
/
Copy pathSorting.java
More file actions
117 lines (91 loc) · 2.35 KB
/
Copy pathSorting.java
File metadata and controls
117 lines (91 loc) · 2.35 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package collectionFramework;
import java.util.*;
public class Sorting {
static class Student implements Comparable<Student> {
int rollNo;
String name;
public Student(int rollNo, String name) {
this.rollNo = rollNo;
this.name = name;
}
public String toString() {
return "Student [rollNo=" + rollNo + ", name=" + name + "]";
}
@Override
public int compareTo(Student that) {
// if(this.rollNo < that.rollNo) return -1;
// if(this.rollNo > that.rollNo) return 1;
// return 0;
return this.rollNo - that.rollNo;
}
}
static class StudentNameComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s1.name.compareTo(s2.name);
}
}
public static void main(String[] args) {
Student s1 = new Student(1, "Rakesh");
Student s2 = new Student(2, "Bob");
Student s3 = new Student(3, "Anuj");
Student s4 = new Student(8, "Anuj");
Student s5 = new Student(12, "Anuj");
ArrayList<Student> list = new ArrayList<>();
list.add(s4);
list.add(s5);
list.add(s2);
list.add(s1);
list.add(s3);
// System.out.println(list);
// Collections.sort(list, new StudentNameComparator());
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.name.equals(o2.name)) {
return o1.rollNo - o2.rollNo;
}
return o1.name.compareTo(o2.name);
}
});
Collections.sort(list, (o1, o2) -> o1.name.compareTo(o2.name));
// Collections.sort(list, new Comparator(Student::getName));
// Comparator<Student> studentNameComparator
// = Comparator.comparing(Student::getName);
// System.out.println(list);
int a[][] = {
{1, 2, 23},
{3, 5, 12},
{2, 7, 1}
};
Arrays.sort(a, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
for(int x[]: a) {
for(int e: x) {
System.out.print(e+" ");
}
System.out.println();
}
List<List<Integer>> anotherList = new ArrayList<List<Integer>>();
for(int i = 0; i<3; i++) {
List<Integer> l = new ArrayList<>();
anotherList.add(l);
}
//
// list.get(0).add(1);
// list.get(0).add(2);
//
// list.get(1).add(3);
// list.get(1).add(5);
//
// list.get(2).add(1);
// list.get(2).add(7);
//
// System.out.println(list);
//
}
}