import java.util.Arrays;
import java.util.Comparator;
public class ArraysSort {
public static void main(String[] args) {
int[] a={2,5,4,3,1,8};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}// 结果// [1, 2, 3, 4, 5, 8]
import java.util.Arrays;
import java.util.Comparator;
public class ArraysSort {
public static void main(String[] args) {
int[] a={2,5,4,3,1,8};
Arrays.sort(a,2,5);
System.out.println(Arrays.toString(a));
}
}
// 结果
// [2, 5, 1, 3, 4, 8]
// 按第一维元素比较二维数组
import java.util.Arrays;
import java.util.Comparator;
public class ArraysSort {
public static void main(String[] args) {
int[][] nums=new int[][]{{1,3},{1,2},{4,5},{3,7}};
//方法一
Arrays.sort(nums,new Comparator(){
@Override
public int compare(int[] a,int[] b){
if(a[0]==b[0]){ // 不明白为什么要这样写 ,自己的基础有问题,不解
return a[1]-b[1];
}else{
return a[0]-b[0];
}
}
});
for (int[] num : nums) {
System.out.println(Arrays.toString(num));
}
}
}
// 结果
/*
[1, 2]
[1, 3]
[3, 7]
[4, 5]
*/
// 按照第二维元素比较二维数组
import java.util.Arrays;
import java.util.Comparator;
public class ArraysSort {
public static void main(String[] args) {
int[][] nums=new int[][]{{1,3},{1,2},{4,5},{3,7}};
//方法一
Arrays.sort(nums,new Comparator(){
@Override
public int compare(int[] a,int[] b){
if(a[1]==b[1]){ //同样不解
return a[0]-b[0];
}else{
return a[1]-b[1];
}
}
});
//方法二
/*Arrays.sort(nums,(a,b)->a[1]-b[1]);*/
for (int[] num : nums) {
System.out.println(Arrays.toString(num));
}
}
}
// 结果
/*
[1, 2]
[1, 3]
[4, 5]
[3, 7]
*/
import java.util.Arrays;
import java.util.Comparator;
class Dog{
int size;
int weight;
public Dog(int s, int w){
size = s;
weight = w;
}
}
class DogSizeComparator implements Comparator{
@Override
public int compare(Dog o1, Dog o2) {
return o1.size - o2.size;
}
}
class DogWeightComparator implements Comparator{ // **在这里尖括号传入的是对象**
@Override
public int compare(Dog o1, Dog o2) {
return o1.weight - o2.weight;
}
}
public class ArraysSort {
public static void main(String[] args) {
Dog d1 = new Dog(2, 50);
Dog d2 = new Dog(1, 30);
Dog d3 = new Dog(3, 40);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new DogSizeComparator());
printDogs(dogArray);
Arrays.sort(dogArray, new DogWeightComparator());
printDogs(dogArray);
}
public static void printDogs(Dog[] dogs){
for(Dog d: dogs)
System.out.print("size="+d.size + " weight=" + d.weight + " ");
System.out.println();
}
}
// 结果
/*
size=2 weight=50 size=1 weight=30 size=3 weight=40
size=1 weight=30 size=2 weight=50 size=3 weight=40
size=1 weight=30 size=3 weight=40 size=2 weight=50
*/
import java.util.Arrays;
import java.util.Comparator;
class Animal{
int size;
}
class Dog extends Animal{
public Dog(int s){
size = s;
}
}
class Cat extends Animal{
public Cat(int s){
size = s;
}
}
class AnimalSizeComparator implements Comparator{
@Override
public int compare(Animal o1, Animal o2) {
return o1.size - o2.size;
}
}
public class ArraysSort {
public static void main(String[] args) {
Dog d1 = new Dog(2);
Dog d2 = new Dog(1);
Dog d3 = new Dog(3);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new AnimalSizeComparator());
printDogs(dogArray);
System.out.println();
Cat c1 = new Cat(2);
Cat c2 = new Cat(1);
Cat c3 = new Cat(3);
Cat[] catArray = {c1, c2, c3};
printDogs(catArray);
Arrays.sort(catArray, new AnimalSizeComparator());
printDogs(catArray);
}
public static void printDogs(Animal[] animals){
for(Animal a: animals)
System.out.print("size="+a.size + " ");
System.out.println();
}
}
// 结果
/*
size=2 size=1 size=3
size=1 size=2 size=3
size=2 size=1 size=3
size=1 size=2 size=3
*/
巨人的肩膀:https://www.cnblogs.com/SupremeBoy/p/12717532.html