java 对象的排序 多字段比较 字符串排序

结果:

[A [a=3, b=1, name=he], A [a=3, b=1, name=hello], A [a=1, b=2, name=null], A [a=2, b=10, name=null], A [a=1, b=1, name=null], A [a=3, b=2, name=null], A [a=3, b=2, name=播], A [a=3, b=2, name=阿], A [a=3, b=2, name=a], A [a=3, b=2, name=b], A [a=1, b=5, name=null], A [a=2, b=3, name=null]]
[A [a=3, b=2, name=播], A [a=3, b=2, name=阿], A [a=3, b=2, name=b], A [a=3, b=2, name=a], A [a=3, b=2, name=null], A [a=3, b=1, name=hello], A [a=3, b=1, name=he], A [a=2, b=10, name=null], A [a=2, b=3, name=null], A [a=1, b=5, name=null], A [a=1, b=2, name=null], A [a=1, b=1, name=null]]
[A [a=1, b=1, name=null], A [a=1, b=2, name=null], A [a=1, b=5, name=null], A [a=2, b=3, name=null], A [a=2, b=10, name=null], A [a=3, b=1, name=he], A [a=3, b=1, name=hello], A [a=3, b=2, name=null], A [a=3, b=2, name=a], A [a=3, b=2, name=b], A [a=3, b=2, name=阿], A [a=3, b=2, name=播]]


package com.kk.android;

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

public class TestComp {

	public static class A {

		public A(int a, int b) {
			super();
			this.a = a;
			this.b = b;
		}

		public A(int a, int b, String name) {
			super();
			this.a = a;
			this.b = b;
			this.name = name;
		}

		@Override
		public String toString() {
			return "A [a=" + a + ", b=" + b + ", name=" + name + "]";
		}

		public int a;
		public int b;
		public String name;
	}

	static final Comparator A_ASC = new Comparator() {

		// 0是一样,-1是o1排前面,1是o2排前面
		@Override
		public int compare(A o1, A o2) {
			if (o1.a == o2.a) {
				if (o1.b == o2.b) {
					if (o1.name == null && o2.name == null) {
						return 0;
					}
					if (o1.name == null && o2.name != null) {
						// null的排前面
						return -1;
					}
					if (o1.name != null && o2.name == null) {
						// null的排前面
						return 1;
					}
					Comparator com = Collator.getInstance(java.util.Locale.CHINA);
					//字符串比较
					return com.compare(o1.name, o2.name);
				} else {
					if (o1.b > o2.b) {
						// b大的排后面
						return 1;
					} else {
						return -1;
					}
				}
			} else {
				if (o1.a > o2.a) {
					// a大的排后面
					return 1;
				} else {
					return -1;
				}
			}
		}
	};
	static final Comparator A_DESC = new Comparator() {

		@Override
		public int compare(A o1, A o2) {
			if (o1.a == o2.a) {
				if (o1.b == o2.b) {
					if (o1.name == null && o2.name == null) {
						return 0;
					}
					if (o1.name == null && o2.name != null) {
						// null的排前面
						return 1;
					}
					if (o1.name != null && o2.name == null) {
						// null的排前面
						return -1;
					}
					Comparator com = Collator.getInstance(java.util.Locale.CHINA);
					//字符串比较
					return com.compare(o2.name, o1.name);
				} else {
					if (o1.b > o2.b) {
						return -1;
					} else {
						return 1;
					}
				}
			} else {
				if (o1.a > o2.a) {
					return -1;
				} else {
					return 1;
				}
			}
		}
	};

	public static void main(String[] args) {
		List as = new ArrayList<>();
		as.add(new A(3, 1, "he"));
		as.add(new A(3, 1, "hello"));
		as.add(new A(1, 2));
		as.add(new A(2, 10));
		as.add(new A(1, 1));
		as.add(new A(3, 2));
		as.add(new A(3, 2, "播"));
		as.add(new A(3, 2, "阿"));
		as.add(new A(3, 2, "a"));
		as.add(new A(3, 2, "b"));
		as.add(new A(1, 5));
		as.add(new A(2, 3));

		System.out.println(as);
		Collections.sort(as, A_DESC);
		System.out.println(as);
		Collections.sort(as, A_ASC);
		System.out.println(as);
	}
}
 
  


你可能感兴趣的:(java 对象的排序 多字段比较 字符串排序)