最好的10个用字符串、数字和对象的Java阵列排序例子

261 阅读4分钟

数组排序实例

Array 是存储在一个名称下的元素的集合。在我之前的文章中,讨论了关于数组的教程和例子

排序是基于ascendingdescending 的顺序。对于数字,Ascending 顺序意味着从最低的数字到最高的数字,即数字将是增加的顺序。降序意味着从最高的数字到最低的数字,即数字将是减少的顺序。

对于字母,A,B,C是自然顺序和升序,C,B,A是升序和降序的相反顺序。

这篇文章是关于java中各种最佳数组排序的例子。

Arrays.sort method java examples

java.util.Arrays.sort方法用于对原始类型以及对象进行排序。

public static void sort(T[] a, Comparator c)

参数是:-

第一个参数是任何对象或原始类型

第二个参数是比较器,这是用于比较数组中的值的函数。这允许返回数组的顺序。

将数组中的数字按升序排序?

在java中,数字可以用整数和长类型来存储,这个例子将一个整数阵列按升序排序

Integer[] numbers = { 5,3,17,1};  
System.out.println(Arrays.toString(numbers));  
Arrays.sort(numbers);  
System.out.println(Arrays.toString(numbers));  

输出结果是

Original:[5, 3, 17, 1]  
Sorted asc order:[1, 3, 5, 17]  

如何对整数数组进行降序排序?

sort() 没有 comparator 该方法将元素按升序返回。要进行降序排序,你需要自己编写比较器或者使用Collections.reverseOrder()

这里提供的比较器是: order Collections.reverse()

Integer[] numbers = { 5,3,17,1};  
System.out.println("Original:"+Arrays.toString(numbers));  
Arrays.sort(numbers,Collections.reverseOrder());  
System.out.println("Sorted desc order:"+Arrays.toString(numbers)); 

输出:

Original:[5, 3, 17, 1]  
Sorted desc order:[17, 5, 3, 1]  

如何对字符串数组进行升序排序?

将字符串数组传递给sort方法,该方法会以升序返回字符串数组。

String[] strs = { "one", "two", "three", "four", "five" ,"five"};  
System.out.println("Original:"+Arrays.toString(strs));  
Arrays.sort(strs);  
System.out.println("Sorted asc order:"+Arrays.toString(strs));  

输出结果

Original:[one, two, three, four, five, five]  
Sorted asc order:[five, five, four, one, three, two]  

如何对字符串数组进行降序排序?

将reverseOrder()比较器传给Array.sort() 方法,按降序排序。

String[] strs = { "one", "two", "three", "four", "five" ,"five"};  
System.out.println("Original:"+Arrays.toString(strs));  
Arrays.sort(strs,Collections.reverseOrder());  
System.out.println("Sorted desc order:"+Arrays.toString(strs));  

输出结果是

Original:[one, two, three, four, five, five]  
Sorted desc order:[two, three, one, four, five, five]  

如何在java中对二维数组进行排序?

二维数组是包含数组的数组。你必须写一个比较器来对二维数组中的元素进行排序
这在所有版本的java中都适用。

  
int[][] arrays = { { 3, 9 }, { 2, 5 }, {18, 19 }, { 41, 3 }, { 15, 4 } };  
System.out.println("Original:"+Arrays.deepToString(arrays));  
Arrays.sort(arrays, new Comparator() {  
 @Override  
 public int compare(int[] o1, int[] o2) {  
 return ((Integer) o2[0]).compareTo(o1[0]);  
 }  
});  
System.out.println("Sorted:"+Arrays.deepToString(arrays));  
  
Or in Java 8 use below code   
int[][] arrays = { { 3, 9 }, { 2, 5 }, {18, 19 }, { 41, 3 }, { 15, 4 } };  
  System.out.println("Original:"+Arrays.deepToString(arrays));  
        Arrays.sort(arrays, Comparator.comparing((int[] arr) -> arr[0])  
                .reversed());  
  
  System.out.println("Sorted:"+Arrays.deepToString(arrays));  

输出是

[[3, 9], [2, 5], [18, 19], [41, 3], [15, 4]]  
[[41, 3], [18, 19], [15, 4], [3, 9], [2, 5]]  

如何在java中对整数数组进行排序和合并?

  • 创建两个具有内联初始化的数组
  • 将两个数组的长度相加,用这个和创建新的数组
  • 使用arrayCopy将两个数组对象复制到新数组中。
  • 最后使用Arrays.sort()方法对新数组进行排序。
Integer array1[] = { 9, 5, 71 };  
Integer array2[] = { 21, 50, 1 };  
Integer output[] = new Integer[array1.length + array2.length];  
System.arraycopy(array1, 0, output, 0, array1.length);  
System.arraycopy(array2, 0, output, array1.length, array2.length);  
Arrays.sort(output);  
System.out.println(Arrays.toString(output));  

如何对自定义对象数组进行升序或降序排序?

创建了带有id和工资字段的Employee对象。

public class Employee implements Comparable {  
 Integer id;  
 Integer salary;  
  
 public Integer getId() {  
  return id;  
 }  
 public void setId(Integer id) {  
  this.id = id;  
 }  
 public Integer getSalary() {  
  return salary;  
 }  
 public void setSalary(Integer salary) {  
  this.salary = salary;  
 }  
 Employee(Integer id, Integer salary) {  
  this.id = id;  
  this.salary = salary;  
 }  
 @Override  
 public int compareTo(Employee e) {  
  return this.salary - e.salary;  
 }  
 @Override  
 public String toString() {  
  return "Employee [id=" + id + ", salary=" + salary + "]";  
 }  
}  
Employee[] emps = new Employee[3];  
emps[0] = new Employee(1, 4000);  
emps[1] = new Employee(2, 60000);  
emps[2] = new Employee(3, 5000);

根据工资的升序对雇员进行排序

Arrays.sort(emps);  
for (int i = 0; i < emps.length; i++) {  
 Employee emp = emps[i];  
 System.out.println(emp);  
}  

Employee [id=1, salary=4000]  
Employee [id=3, salary=5000]  
Employee [id=2, salary=60000]  

根据工资的降序对雇员进行排序

Arrays.sort(emps, Collections.reverseOrder());  
for (int i = 0; i < emps.length; i++) {  
 Employee emp = emps[i];  
 System.out.println(emp);  
}  

输出结果是

Employee [id=2, salary=60000]  
Employee [id=3, salary=5000]  
Employee [id=1, salary=4000]  

总结

你学到了一些关于在java中以升序和降序进行数组排序的基本例子