import
java.util.*;
class
Laptop {
int
ModalNo;
String name;
int
ram;
Laptop(
int
ModalNo, String name,
int
ram)
{
this
.ModalNo = ModalNo;
this
.name = name;
this
.ram = ram;
}
}
class
RamComparator
implements
Comparator<Laptop> {
public
int
compare(Laptop l1, Laptop l2)
{
if
(l1.ram == l2.ram) {
return
0
;
}
else
if
(l1.ram < l2.ram) {
return
1
;
}
else
{
return
-
1
;
}
}
}
class
GFG {
public
static
void
main(String[] args)
{
ArrayList<Laptop> l =
new
ArrayList<Laptop>();
l.add(
new
Laptop(
322
,
"Dell"
,
2
));
l.add(
new
Laptop(
342
,
"Asus"
,
8
));
l.add(
new
Laptop(
821
,
"HP"
,
16
));
l.add(
new
Laptop(
251
,
"Lenovo"
,
6
));
l.add(
new
Laptop(
572
,
"Acer"
,
4
));
System.out.println(
"before sorting"
);
System.out.println(
"Ram"
+
" "
+
"Name"
+
" "
+
"ModalNo"
);
for
(Laptop laptop : l) {
System.out.println(laptop.ram +
" "
+ laptop.name +
" "
+ laptop.ModalNo);
}
System.out.println();
System.out.println(
"After sorting(sorted by Ram)"
);
System.out.println(
"Ram"
+
" "
+
"Name"
+
" "
+
"ModalNo"
);
Collections.sort(l,
new
RamComparator());
for
(Laptop laptop : l) {
System.out.println(laptop.ram +
" "
+ laptop.name +
" "
+ laptop.ModalNo);
}
}
}