First Blog: Link List Sorting

Posted by Mr_J on Sun, 21 Jul 2019 12:52:41 +0200

This is my first blog, saying that learning is 50% of learning, and the other 50% is explanations. I will share some problems I encounter in the future. Welcome to your blogs for advice.
Today, we encounter the problem of sorting object arrays in data processing, which can be summarized as follows:

I. The data stored in the linked list is string data.

1. The method of Collections.sort(list) can be used directly to sort strings in lexicographic order, and the method of Collections.reverse(list) can be used to sort strings in lexicographic order. (Because the String class has implemented the Comparable interface)

		ArrayList<String> list=new ArrayList<String>();
		list.add("123");
		list.add("2");
		list.add("13");
		Collections.sort(list);


2. If the above sorting can not meet our requirements, then we need to customize the sorting method to sort the collection. Custom sorting needs to implement the Comparator interface, and rewrite the sorting method int compare(String s1,String s2)
(There is a method int compare (TS 1, t s2) in the Comparator interface. The return value of this method is int, indicating that the comparison results s1 and s2, if the return value is less than 0, then s1 < s2; the return value is equal to 0, s1=s2; the return value is greater than 0, s1 > s2.

		ArrayList<String> list=new ArrayList<String>();
		list.add("123");
		list.add("2");
		list.add("13");
		Collections.sort(list,new Comparator<String>() {
			public int compare(String o1,String o2)
			{
				int flag=1;
				if(Integer.parseInt(o1)<Integer.parseInt(o2))
					flag=-1;
				if(Integer.parseInt(o1)==Integer.parseInt(o2))
					flag=0;
				return flag;
			}
		});

2. The data stored in the linked list is object data.

In this case, the data stored in the linked list is of String type. The author believes that the processing method is the same, but only to find a member variable on the basis of the object, and then sort it according to it. Example: Numbers of Count objects are arranged in ascending order of data size. In descending order, only the compare method needs to be processed.

public class Count{
	private String number;

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}}
	ArrayList<Count> list2=new ArrayList<Count>();
	Count a=new Count();a.setNumber("13");list2.add(a);
	Count b=new Count();b.setNumber("123");list2.add(b);
	Count c=new Count();c.setNumber("2");list2.add(c);
	Collections.sort(list2);
	Collections.sort(list2,new Comparator<Count>() {
		public int compare(Count o1,Count o2)
		{
			int flag=-1;
			if(Integer.parseInt(o1.getNumber())<Integer.parseInt(o2.getNumber()))
				flag=1;
			if(Integer.parseInt(o1.getNumber())==Integer.parseInt(o2.getNumber()))
				flag=0;
			return flag;
		}
	});

3. Differences between Comparable and Compparator Java Comparator s

Comparator is very important in sorting object arrays. There are some differences between them. Because the Comparable interface is designed to allow the class to implement the interface, if it is not considered in the design of the class, then the sorting function can be achieved through Comparator. The difference between the two interfaces that need to be rewritten is that the corresponding sorting method of Comparable interface is int compareTo(Object b), and the return value is taken. Represents the same size comparison as int Compare(T s1,t s2). Obviously, the number of parameters is different between them. Here's an example of using Comparable:

package dateProcess;

public class Count implements Comparable{
	private String number;

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
	
	public int compareTo(Object b)
	{
		Count s=(Count) b;
		int flag=1;
		if(Integer.parseInt(this.getNumber())<Integer.parseInt(s.getNumber()))
			flag=-1;
		if(Integer.parseInt(this.getNumber())==Integer.parseInt(s.getNumber()))
			flag=0;
		return flag;
	}

}
ArrayList<Count> list2=new ArrayList<Count>();
		Count a=new Count();a.setNumber("13");list2.add(a);
		Count b=new Count();b.setNumber("123");list2.add(b);
		Count c=new Count();c.setNumber("2");list2.add(c);
		Collections.sort(list2);

Topics: Java less