List collection sorting in java

    In java, sometimes we may need to sort a property of the objects in the list collection. This problem is very simple. The Collections tool class provides a sorting method. There are two overloaded methods. One is to accept only a List collection, and the other is to accept a list and a Comparator interface. I will explain the second one here as an example, and I will briefly explain the first one later. Note that the default order is ascending order .

    In the java api, you can see that there is only one method in the Comparator interface, int compare( T  o1,   T  o2), which passes in two objects. Regarding the return value, there are three cases - 1, 0, 1. -1 means o1 is less than o2, 0 means o1 is equal to o2, 1 means o1 is greater than o2. If what we want is to sort in ascending order, then compare the o2 attributes of o1 and ensure that o1 is less than o2 and returns -1, o1 equals o2 and returns 0, and o1 is greater than o2 and returns 1, and the result is in ascending order. Knotted together. It is also possible to want to descend in descending order. We only need to understand that the program can only determine the order by the value returned by the compare method. When -1, o1 is in the front, 1 is in the back, and 0 does not need to be moved. The sorting method of Collections is always based on Returns the value "in ascending order". To put it bluntly, when the compare method returns -1, it means that o1 is in front of o2, and returning 1 means that o1 is in the back of o2. As for whether to return -1 or 1, it is determined according to business requirements.

 Here's an example:

 

package com.pzn.sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortListTest {
	
	public static void main(String[] args) {
		
		List<Per> list = new ArrayList<>();
		list.add(new Per("a", "2015-11-10"));
		list.add(new Per("b", "2015-10-10"));
		list.add(new Per("c", "2015-09-10"));
		list.add(new Per("d", "2015-11-02"));
		list.add(new Per("e", "2015-10-13"));
		// ascending order
		Collections.sort(list, new Comparator<Per>() {
			public int compare(Per o1, Per o2) {
				return o1.bir.compareTo(o2.one);
			}
		});
		
		System.out.println(list);
		// descending order
		Collections.sort(list, new Comparator<Per>() {
			public int compare(Per o1, Per o2) {
				return -o1.bir.compareTo(o2.one);
			}
		});
		
		System.out.println(list);
		
	}
}

class Per {
	
        // Don't ask me why I don't write private here, and then provide setter/getter methods, but use public.
        // Because I was lazy when I wrote the test code, and this comment was added when I wrote the blog.
	public final String name;
	public final String bir;
	
	public Per(String name, String bir) {
		this.name = name;
		this.one = one;
	}

	@Override
	public String toString() {
		return "Per [name=" + name + ", bir=" + bir + "]";
	}
}

 

 

 

 Finally, let’s talk about the first sorting method. You only need to pass in the method of the list collection. This method requires that the class to which the objects in the list collection to be sorted belong must implement a Comparable接口,可能也注意到了,上面代码中String的比较就是使用的一个compareTo method. This method comes from the Comparable interface, that is, String implements this interface.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326493831&siteId=291194637