Created
September 3, 2018 03:06
-
-
Save Delf-Lee/81ba24ef5e394011ef4bbac85de28180 to your computer and use it in GitHub Desktop.
Java object sort example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package _workspace; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
Student[] arr = new Student[5]; | |
List<Student> list = new ArrayList<>(); | |
arr[0] = new Student("A", 26, "컴퓨터공학"); | |
arr[1] = new Student("B", 24, "컴퓨터공학"); | |
arr[2] = new Student("C", 27, "기계공학"); | |
arr[3] = new Student("D", 21, "컴퓨터공학"); | |
arr[4] = new Student("E", 20, "기계공학"); | |
for (int i = 0; i < arr.length; i++) { | |
list.add(arr[i]); | |
} | |
Arrays.sort(arr); | |
Collections.sort(list); | |
System.out.println("* Plaint array print"); | |
for (int i = 0; i < arr.length; i++) { | |
System.out.println(arr[i]); | |
} | |
System.out.println("\n* ArrayList print"); | |
for (int i = 0; i < list.size(); i++) { | |
System.out.println(list.get(i)); | |
} | |
} | |
} | |
class Student implements Comparable<Student> { | |
public String name; | |
public int age; | |
public String department; | |
public Student(String name, int age, String department) { | |
this.name = name; | |
this.age = age; | |
this.department = department; | |
} | |
@Override | |
public int compareTo(Student o) { | |
return this.age - o.age; | |
} | |
@Override | |
public String toString() { | |
return name + "(" + age + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment