Array.sort(arr)可以进行简单的排序,如果需要复杂的排序可以实现Comparable
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.tj; 2 3 import java.util.Arrays; 4 5 public class User implements Comparable<User> { 6 7 Integer id; 8 String username; 9 String password; 10 Integer age; 11 12 public User(Integer id, String username, String password, Integer age) { 13 this.id = id; 14 this.username = username; 15 this.password = password; 16 this.age = age; 17 } 18 19 @Override 20 public int compareTo(User o) { 21 if (this.age > o.age) { 22 return 1; 23 } else if (this.age < o.age) { 24 return -1; 25 } else { 26 return 0; 27 } 28 } 29 30 @Override 31 public String toString() { 32 return this.username + "\t" + this.age; 33 } 34 35 public static void main(String[] args) { 36 User[] users = new User[5]; 37 users[0] = new User(1,"malin1","123",10); 38 users[1] = new User(1,"malin2","123",16); 39 users[2] = new User(1,"malin3","123",15); 40 users[3] = new User(1,"malin4","123",14); 41 users[4] = new User(1,"malin5","123",11); 42 43 Arrays.sort(users); 44 for (User user : users) { 45 System.out.println(user); 46 } 47 } 48 49 }