List sort(Comparator<? super E> c)
public void sort(Comparator<? super E> c)
Description copied from interface: List
Sorts this list according to the order induced by the specified Comparator.
All elements in this list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
If the specified comparator is null then all elements in this list must implement the Comparable interface and the elements' natural ordering should be used.
This list must be modifiable, but need not be resizable.
Specified by:sort in interface List<E>Parameters:c - the Comparator used to compare list elements. A null value indicates that the elements' natural ordering should be used
For Example:
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- public class MapDemo {
- public static void main(String[] args) {
-
- //sort Object type
- List<Student> list = new ArrayList<Student>();
- list.add(new Student("Jack", 30));
- list.add(new Student("Tom", 35));
- list.add(new Student("ROSE", 20));
- list.sort(new Comparator<Student>() {
- @Override
- public int compare(Student stu1, Student stu2) {
- //according student age ASC
- return stu1.getAge().compareTo(stu2.getAge());
- }
-
- });
-
- //print student name and age
- for (Student s : list) {
- System.out.println(s.getName()+"==="+s.getAge());
- }
-
- //sort basic type
- List<Integer> list2 = new ArrayList<Integer>();
- list2.add(20);
- list2.add(18);
- list2.add(30);
- list2.sort(new Comparator<Integer>() {
- @Override
- public int compare(Integer num1, Integer num2) {
- return num1.compareTo(num2);
- }
-
- });
-
- //print number
- for (Integer num : list2) {
- System.out.println(num);
- }
- }
- }
thnx
ReplyDeleteTo be frank, it's turkish to me, but anyway I find it very inspirational, I would say! Obviously, I know things on Java, but machine learning has been the very thing that makes me mazed since I've got to know about it about a year ago. Actually, this is the very reason I've decided to start my way with programing! Now I'm standing with this resource that has a great amount of tutorials on common aspects of java core https://explainjava.com/install-update-remove-java/ provided here. Anyway, I'll check your github and maybe after some swotting things would become more clear to me
ReplyDelete