자바 초보부터 B형까지 - 다양한 기준으로 정렬하기 위한 객체정렬 배우기

1. custom comparator

 

국어, 영어, 수학 점수를 포함한 학생 5명의 정보가 주어질때, 국어 점수를 기준으로 오름차순 정렬하는 방법은?

 

자바에서는 custom comparator로 직접 만들어야한다

 

반환 타입이 반드시 int여야하며, 정렬을 위한 객체 class를 타입으로 하는 1개의 인자를 가지고 있어야한다.

 

정렬을 위한 객체 뒤에 implements Comparable<class type 이름>을 붙이고

 

public int compareTo 함수를 해당 class 안에 override annotator와 함께 적어준다.

 

class Student implements Comparable<Student> {
    int kor, eng, math;

    public Student(int kor, int eng, int math){
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }

    @Override
    public int compareTo(Student student) {
        //국어 점수 기준 오름차순 정렬

        if(this.kor > student.kor){
            return 1;
        } else if(this.kor < student.kor){
            return -1;
        } else {
            return 0;
        }
    }
}

 

현재 객체 this에 들어있는 값과, 함수 compareTo로 넘어온 객체 값을 비교하여 정렬하고자 할때,

 

오름차순 정렬을 하고 싶다면...

 

1) 현재 객체가 정렬 기준 더 뒤에 나와야 한다면, 0보다 큰 값을 반환해야 한다.

 

2) 현재 객체가 정렬 기준 더 앞에 나와야 한다면, 0보다 작은 값을 반환해야 한다.

 

3) 현재 객체가 정렬 기준 우선순위가 함수로 넘어온 객체와 동일하다면, 값 0을 반환해야한다.

 

---------------------------------------------------------------------------------------------------------------------

 

 

@Override
public int compareTo(Student student) { // 국어 점수 기준 오름차순 정렬
    if(this.kor > student.kor)
        return 1;
    else if(this.kor < student.kor)
        return -1;
    else
        return 0;
}

 

 

위 코드는 국어 점수를 기준으로 오름차순 정렬을 하기 위한 코드이다.

 

현재 객체의 국어 점수가 더 뒤에 나와야 하는 경우(더 큰 경우) this.kor > student.kor에 대해서는 0보다 큰 값인 1을 반환해주고,

 

반대의 경우(더 작은 경우) -1을 반환해주며, 두 값이 동일하면 0을 반환해준다.

 

혹은 다음과 같이 간단하게 작성이 가능할 것이다

 

    @Override
    public int compareTo(Student student){
        //국어 점수 기준 오름차순 정렬
        return this.kor - student.kor;
    }

 

이러한 custom comparator가 작성되어 있을때, Arrays.sort를 수행하면 국어 점수 기준으로 오름차순 정렬이 가능하다

 

import java.util.Arrays;

class Student implements Comparable<Student> {
    int kor, eng, math;

    public Student(int kor, int eng, int math){
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }

    @Override
    public int compareTo(Student student){
        //국어 점수 기준 오름차순 정렬
        return this.kor - student.kor;
    }
}
public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.
        Student[] students = new Student[] {
            new Student(90,80,90), //첫번째 학생
            new Student(20,80,80), //두번째 학생
            new Student(90,30,60), //세번째 학생
            new Student(60,10,50), //네번째 학생
            new Student(80,20,10) //다섯번째 학생
        };

        Arrays.sort(students); //국어 점수 기준 오름차순 정렬

        for(int i = 0; i < 5; i++){
            System.out.println(students[i].kor + " " + students[i].eng + " " + students[i].math);
        }

        
    }
}

20 80 80
60 10 50
80 20 10
90 80 90
90 30 60

 

여기서 국어 점수가 90점으로 동일한 두 학생이 있는데, 이런 경우 어떤 학생이 더 앞서서 나와야 하는지 기준이 없어서,

 

더욱 명확한 기준이 필요할 수도 있다.

 

예를 들어 국어 점수를 기준으로 내림차순 정렬을 해보면...

 

@Override
public int compareTo(Student student) {
//국어 점수 기준 내림차순 정렬
    return student.kor - this.kor;
}

90 80 90
90 30 60
80 20 10
60 10 50
20 80 80

 

아니 내가 원하는건 이게 아닌데

 

국어 점수가 동일할때, 영어 점수가 높을수록 먼저 나오도록 정렬하고 싶다면...

 

국어 점수가 동일한 if문에서 영어 점수 정렬기준을 if문으로 세워주면 되겠지

 

@Override
public int compareTo(Student student) { // 국어 점수 기준 오름차순 정렬
    if(this.kor > student.kor)
        return 1;
    else if(this.kor < student.kor)
        return -1;
    else
        //국어 점수가 동일할때, 영어 점수 기준으로 내림차순 정렬
        if(this.eng < student.eng){
            return 1;
        } else if(this.eng > student.eng){
            return -1;
        } else {
            return 0;
        }
}

 

 

 

2. 객체 정렬은 왜 필요한가

 

객체 정렬을 사용하지 않은 경우, 국어 영어 수학 점수를 서로 다른 리스트로 전부 따로 관리해야하는데,

 

이 경우 국어 점수를 오름차순으로 정렬한다면, 당연히 해당 학생의 다른 과목 점수도 순서가 바뀜에 따라 같이 움직여야하는데,

 

따로 관리하면 국어 점수만 정렬이 될 뿐 다른 점수는 정렬이 안된다.

 

어떻게 방법이 있겠지만 상당히 어려울듯

 

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] kors = new int[]{90, 20, 90, 60, 80};
        int[] engs = new int[]{80, 80, 30, 10, 20};
        int[] maths = new int[]{90, 80, 60, 50, 10};

        Arrays.sort(kors);

        for(int i = 0; i < 5; i++)       // 20, 60, 80, 90, 90
            System.out.print(kors[i] + " ");
        System.out.println();

        for(int i = 0; i < 5; i++)       // 80, 80, 30, 10, 20
            System.out.print(engs[i] + " ");
        System.out.println();

        for(int i = 0; i < 5; i++)       // 90, 80, 60, 50, 10
            System.out.print(maths[i] + " ");
        System.out.println();
    }
}

 

3. Arrays.sort의 두번째 인자

 

java 8부터는 Arrays.sort()의 두번째 인자로 정렬함수를 넣어준다면, 해당 정렬함수 기준으로 정렬을 수행해준다.

 

//국어 점수 기준 오름차순 정렬
Arrays.sort(students, (a,b) -> a.kor - b.kor);

 

두 인자 값을 순서대로 a,b라 해서 국어 점수 기준으로 오름차순 정렬을 하고 싶다면, a.kor - b.kor을 반환해준다.

 

 

import java.util.Arrays;

class Student{
    int kor, eng, math;

    public Student(int kor, int eng, int math){
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }
}
public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.
        Student[] students = new Student[] {
            new Student(90, 80, 90), //첫번째 학생
            new Student(20, 80, 80), //두번째 학생
            new Student(90, 30, 60), //세번째 학생
            new Student(60, 10, 50), //네번째 학생
            new Student(80, 20, 10), //다섯번째 학생
        };

        //국어 점수 기준 오름차순 정렬
        Arrays.sort(students, (a,b) -> a.kor - b.kor);

        for(int i = 0; i < 5; i++){
            System.out.println(students[i].kor + " " + students[i].eng + " " + students[i].math);
        }

    }
}

20 80 80
60 10 50
80 20 10
90 80 90
90 30 60

 

 

Arrays.sort()의 두번째 인자로 custom comparator를 새로 정의해서 넣어줄 수도 있다.

 

import java.util.Comparator;로 Comparator를 import 받아야함

 

그리고 메소드에 Override를 해야함

 

이름은 compare

 

반환 타입은 int

 

import java.util.Comparator;

// custom comparator를 이용한 정렬
Arrays.sort(students, new Comparator<Student>() {
    @Override
    public int compare(Student a, Student b) {
    //키를 기준 오름차순 정렬
        return a.height - b.height;
    }
});

 

다음은 학생 클래스를 정의하고, 키를 기준으로 오름차순 정렬하는 코드

 

import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;

class Student {

    String name;
    int height;
    int weight;

    public Student(String name, int height, int weight){

        this.name = name;
        this.height = height;
        this.weight = weight;

    }

}
public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        Student[] students = new Student[n];

        for(int i = 0; i < n; i++){
            String name = sc.next();
            int height = sc.nextInt();
            int weight = sc.nextInt();

            students[i] = new Student(name, height, weight);
        }

        Arrays.sort(students, new Comparator<Student>(){

            @Override
            public int compare(Student a, Student b){
                return a.height - b.height;
            }
        });

        for(int i = 0; i < n; i++){
            System.out.println(students[i].name + " " + students[i].height + " " + students[i].weight);
        }
    }
}

 

 

4. 우선순위가 여러개인 객체 정렬

 

국어, 영어, 수학 점수를 포함한 학생 5명의 정보가 주어졌을때, 국어 점수를 기준으로 오름차순 정렬하되,

 

국어 점수가 같다면 영어 점수를 기준으로 오름차순 정렬하는 방법이란?

 

이는 custom comparator를 정의한 함수에서, 여러 기준을 적용해주면 된다.

 

다음은 국어 점수를 기준으로 오름차순 정렬하는 함수이다.

 

@Override
public int compareTo(Student student) {
    //오름차순 정렬
    return this.kor - student.kor;
}

 

여기에 국어 점수가 일치하는 경우, 영어 점수를 기준으로 오름차순 정렬하는 코드를 추가하면 된다.

 

@Override
public int compareTo(Student student) {
    
    if(this.kor == student.kor){  //국어 점수가 일치한다면
        return this.eng - student.eng; //영어 점수를 기준으로 오름차순 정렬
    }
    
    //국어 점수가 다르다면, 오름차순 정렬
    return this.kor - student.kor;
}

 

이러면 국어 점수가 같은 경우에 영어 점수를 기준으로 비교해서 오름차순 정렬 된다.

 

import java.util.Arrays;

class Student implements Comparable<Student> {
    int kor, eng, math;

    public Student(int kor, int eng, int math){
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }

    @Override
    public int compareTo(Student student){
        if(this.kor == student.kor){ //국어 점수가 일치한다면,
            return this.eng - student.eng; //영어 점수를 기준으로 오름차순 정렬
        }
        return this.kor - student.kor; //국어 점수가 다르다면, 오름차순 정렬
    }
}
public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.
        Student[] students = new Student[] {
            new Student(90, 80, 90), //첫번째 학생
            new Student(20, 80, 80), //두번째 학생
            new Student(90, 30, 60), //세번째 학생
            new Student(60, 10, 50), //네번째 학생
            new Student(80, 20, 10) //다섯 번째 학생
        };

        Arrays.sort(students);

        for(int i = 0; i < 5; i++){
            System.out.println(students[i].kor + " " + students[i].eng + " " + students[i].math);
        }
    }
}

20 80 80
60 10 50
80 20 10
90 30 60 //국어 점수가 동일하면, 영어 점수 오름차순 기준으로 정렬
90 80 90

 

만약 국어 점수를 오름차순으로 정렬하되, 국어 점수가 동일한 경우에는 영어 점수를 내림차순으로 정렬하고 싶은 경우라면,

 

영어 점수만 내림차순이 되도록 compareTo 함수를 다음과 같이 작성하면 된다

 

@Override
public int compareTo(Student student) {
    if(this.kor == student.kor){ //국어 점수가 일치한다면,
        return student.eng - this.eng; //영어 점수를 기준으로 내림차순 정렬
    }
    return this.kor - student.kor; //국어 점수가 다르다면, 오름차순 정렬
}

 

이 경우에는 국어 점수가 같은 경우, 영어 점수를 기준으로 비교하여 내림차순 정렬 된다.

 

import java.util.Arrays;

class Student implements Comparable<Student> {
    int kor, eng, math;
    
    public Student(int kor, int eng, int math){
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }
    
    @Override
    public int compareTo(Student student){
        if(this.kor == student.kor){ //국어 점수가 일치한다면,
            return student.eng - this.eng; //영어 점수를 기준으로 내림차순 정렬
        }
        return this.kor - student.kor; //국어 점수가 다르다면, 오름차순 정렬
    }
}

public class Main {
    public static void main(String[] args) {
        Student[] students = new Student[] {
            new Student(90, 80, 80), //첫번째 학생
            new Student(20, 80, 80), //두번째 학생
            new Student(90, 30, 60), //세번째 학생
            new Student(60, 10, 50), //네번째 학생
            new Student(80, 20, 10) //다섯번째 학생
        };
        
        Arrays.sort(students);
        
        for(int i = 0; i < 5; i++){
            System.out.println(students[i].kor + " " + students[i].eng + " " +students[i].math);
        }
    }
}

20 80 80
60 10 50
80 20 10
90 80 90 //국어 점수가 동일하면, 영어 점수 기준 내림차순 정렬
90 30 60

 

만약 국어 점수, 영어 점수, 수학 점수 순으로 내림차순 정렬하라고 한다면?

 

구체적으로, 

 

국어 점수가 동일하다면, 영어 점수를 비교해서, 영어 점수마저 동일하다면 수학 점수 순으로 내림차순 정렬하고,

 

영어 점수가 다르다면 영어 점수 순으로 내림차순 정렬한다.

 

반대로 국어 점수가 동일하지 않다면, 국어 점수 순으로 내림차순 정렬한다

 

@Override
public int compareTo(Student student){
    if(this.kor == student.kor){ //국어 점수가 동일하다면
        if(this.eng == student.eng){ //영어 점수가 동일하다면,
            return student.math - this.math; //수학 점수 순으로 내림차순 정렬
        }
        return student.eng - this.eng; //영어 점수가 다르다면, 영어 점수 순으로 내림차순 정렬
    }
    return student.kor - this.kor; //국어 점수가 다르다면, 국어 점수 순으로 내림차순 정렬
}

 

5. 정렬 기준이 복잡한 경우

 

국어, 영어, 수학 점수를 포함한 5명의 학생 정보가 주어졌을때, 점수의 총 합을 기준으로 정렬하고 싶다면?

 

compareTo 함수의 기준을 점수의 총합으로 적어주면 될 것이다.

 

import java.util.Scanner;
import java.util.Arrays;

class Student implements Comparable<Student> {
    String name;
    int kor, eng, math;

    public Student(String name, int kor, int eng, int math){
        this.name = name;
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }
    
    //총점을 기준으로 오름차순 정렬
    @Override
    public int compareTo(Student student){
        return (this.kor + this.eng + this.math) - (student.kor + student.eng + student.math);
    }
}
public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        Student[] students = new Student[n];

        for(int i = 0; i < n; i++){
            students[i] = new Student(sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt());
        }

        Arrays.sort(students);

        for(int i =0; i < n; i++){
            System.out.println(students[i].name + " " + students[i].kor + " " + students[i].eng + " " + students[i].math );

        }


    }
}

 

 

6. 정렬할때 index 찾기

 

국어, 영어, 수학 점수를 포함한 학생 5명의 정보가 주어질때, 국어 점수를 기준으로 내림차순 정렬 하는데 

 

1등부터 5등까지 각 등수에 해당하는 학생의 번호를 출력하는 코드를 작성하라 한다면?

 

정렬하면서 각 학생의 번호도 같이 움직여야 학생의 번호를 출력할 수 있으므로, 객체에 학생의 번호를 나타내는 변수도 추가

 

import java.util.Arrays;

class Student implements Comparable<Student> {
    int kor, eng, math, number;

    public Student(int kor, int eng, int math, int number){
        this.kor = kor;
        this.eng = eng;
        this.math = math;
        this.number = number;
    }

    @Override
    public int compareTo(Student student){
        return student.kor - this.kor;
    }
}

public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.

        Student[] students = new Student[] {
            new Student(90, 80, 90, 1), //첫번째 학생
            new Student(20, 80, 80, 2), //두번째 학생
            new Student(90, 30, 60, 3), //세번째 학생
            new Student(60, 10, 50, 4), //네번째 학생
            new Student(80, 20, 10, 5) //다섯번째 학생
        };

        Arrays.sort(students); //국어 점수 기준 내림차순 정렬

        //정렬 이후 등수별 학생 번호 출력

        for(int i = 0; i < 5; i++){
            System.out.println((i+1) + "등: " + students[i].number + "번");
        }
    }
}

1등: 1번
2등: 3번
3등: 5번
4등: 4번
5등: 2번

 

 

정렬하고 나서 각 번호가 어디로 갔는지를 출력하라고 한다면 어떻게 해야할까?

 

예를 들어 위 예시에서 각 학생의 번호는 등수별로 1 3 5 4 2인데

 

1번이 1등, 2번이 5등 3번이 2등 4번이 4등 5번이 3등이 되었으므로...

 

1 5 2 4 3으로 출력하고 싶다.

 

즉 등수 별로 학생 번호가 1 3 5 4 2가 주어지면, 학생별 등수 1 5 2 4 3을 어떻게 쉽게 찾을 수 있을까?

 

이런 경우, 어느 학생이 어떤 등수를 받았는지를 나타내는 배열을 새로 하나 만들어준다.

 

등수별로 학생 번호 1 3 5 4 2를 순회하면서 각 학생 번호의 index에 해당 rank를 넣어주는 식으로 코드를 작성

 

int[] num_to_rank = new int[6]; //5명이므로
int[] nums = new int[]{1,3,5,4,2}; //등수(index) : 학생번호(value)

for(int i = 0; i < 5; i++){
    //학생번호(index) : 등수(value)
    num_to_rank[nums[i]] = (i+1);
}

for(int i = 1; i <=5; i++){
    System.out.print(num_to_rank[i] + " "); //1 5 2 4 3
}

 

7. 연습문제1

 

5명의 이름, 키, 몸무게가 주어지면 이름순으로 정렬하여 출력하고, 또한 키가 큰 순으로 정렬하여 출력하는 프로그램 작성

 

클래스 안에 comparator를 적을거면 compareTo라고 적고, override를 해야하는데

 

인자로는 Student student 하나만 받으니까.. 

 

이거 클래스 안에 이름 순으로 정렬하는 comparator와 키 순으로 정렬하는 2개 적을수가 없다..

 

그래서 클래스 안에 이름 순으로 정렬하는 comparator 하나 쓰고,

 

키 순으로 정렬하기 위해서 Arrays.sort의 두번째 인자에 키 순으로 정렬하는 custom comparator를 적어준다.

 

-------------------------------------------------------------------------------------------------------------------------------------------------

 

자바 초보에서 B형까지5 -문자열 필수지식- (tistory.com)

 

자바 초보에서 B형까지5 -문자열 필수지식-

1. 공백이 없는 문자열 입력받기 문자열의 타입은 String이고 공백이 없는 문자열은 sc.next()로 입력받을 수 있다. import java.util.Scanner; public class Main { public static void main(String[] args) { // 여기에 코드를

deepdata.tistory.com

 

 

참고로 문자열을 정렬하는 메소드는 compareTo

 

str1.compareTo(str2)가 양수이면, str2가 사전순으로 앞서고

 

음수이면 str1이 사전순으로 앞서고, 0이면 둘이 일치하고

 

어쨌든 compareTo에 그냥 return str1.compareTo(str2); 하면 알아서 정렬해준다

 

import java.util.Scanner;
import java.util.Arrays;

class Student implements Comparable<Student> {
    String name;
    int height;
    float weight;

    public Student(String name, int height, float weight){
        this.name = name;
        this.height = height;
        this.weight = weight;
    }

    @Override
    public int compareTo(Student student){
        return this.name.compareTo(student.name);
    }
}
public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.

        Scanner sc = new Scanner(System.in);

        Student[] students = new Student[5];

        for(int i = 0; i< 5; i++){
            students[i] = new Student(sc.next(), sc.nextInt(), sc.nextFloat());
        }

        Arrays.sort(students);

        System.out.println("name");

        for(int i = 0; i< 5; i++){
            System.out.println(students[i].name + " " + students[i].height + " " + students[i].weight);
        }

        System.out.println();

        Arrays.sort(students, (a,b) -> b.height - a.height);

        System.out.println("height");

        for(int i = 0; i < 5; i++){
            System.out.println(students[i].name + " " +students[i].height + " " + students[i].weight);
        }
    }
}

 

 

혹은 다음과 같이 class 안에 comparator를 정의하지 말고, Arrays.sort의 두번째 인자에 각각 comparator를 작성해준다

 

이 때는 import java.util.Comparator;를 해줘야한다.

 

그리고 함수 이름은 compare로 해주고, @Override를 작성해줘야한다.

 

import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;

//학생들의 정보를 나타내는 클래스
class Student {
    String name;
    int height;
    double weight;

    public Student(String name, int height, double weight){
        this.name = name;
        this.height = height;
        this.weight = weight;
    }
}


public class Main {
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.
        Scanner sc = new Scanner(System.in);

        //변수 선언 및 입력
        int n = 5;
        Student[] students = new Student[n];
        for(int i = 0; i < n; i++){
            String name = sc.next();
            int height = sc.nextInt();
            double weight = sc.nextDouble();

            //Student 객체를 생성해 리스트에 추가
            students[i] = new Student(name, height, weight);
        }
    

    //custom comparator로 정렬
    Arrays.sort(students, new Comparator<Student>(){
        @Override
        public int compare(Student a, Student b){
            //이름 기준 오름차순 정렬
            return a.name.compareTo(b.name);
        }
    });

    //이름순 정렬한 결과 출력
    System.out.println("name");
    for(int i = 0; i < n; i++){
        System.out.print(students[i].name+" ");
        System.out.print(students[i].height + " ");
        System.out.printf("%.1f\n", students[i].weight);
    }

    System.out.println();

    //custom comparator를 이용한 정렬
    Arrays.sort(students, new Comparator<Student>(){
        @Override
        public int compare(Student a, Student b){
            //키 기준 내림차순 정렬
            return b.height - a.height;
        }
    });

    //키 순으로 정렬한 결과
    System.out.println("height");
    for(int i = 0; i < n; i++){
        System.out.print(students[i].name + " ");
        System.out.print(students[i].height + " ");
        System.out.printf("%.1f\n", students[i].weight);
    }
}
}

 

 

8. 연습문제2

 

주어진 좌표 x,y와 원점까지의 거리가 작은 점의 번호를 순서대로 출력해보는 프로그램 작성

 

절댓값 처리를 위해 거리를 구하는 함수 distance를 직접 작성

 

 

8-1) 함수 작성할 때는 public static (반환타입) (이름)(인자) { 내용 }

 

함수는 반드시 Main class내에서는 어디서든 작성 가능

 

Main class 밖에서는 에러남

 

 

 

8-2) Arrays.sort()의 두번째 인자로 new Comparator 사용하기

 

import java.util.Comparator; 가져와야하고 new Comparator<객체 타입>() { 내용 }

 

내용 안에 @Override를 붙이고, public int compare(Point a, Point b)를 작성하여 구현 가능

 

import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;

class Point{
    int x,y,num;

    public Point(int x, int y, int num){
        this.x = x;
        this.y = y;
        this.num = num;
    }
}

public class Main {
    
    public static int distance(Point point){

        if (point.x >= 0 && point.y >= 0){
            return point.x+point.y;
        } else if(point.x >= 0 && point.y < 0){
            return point.x - point.y;
        } else if(point.x < 0 && point.y >= 0){
            return -point.x + point.y;
        } else {
            return -point.x - point.y;
        }
        
    }
    public static void main(String[] args) {
        // 여기에 코드를 작성해주세요.
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        Point[] points = new Point[n];

        for(int i = 0; i < n; i++){
            points[i] = new Point(sc.nextInt(),sc.nextInt(),i+1);
        }

        Arrays.sort(points, new Comparator<Point>() {

            @Override
            public int compare(Point a, Point b){
            if (distance(a) == distance(b)){
                return a.num - b.num;
            }
            return distance(a) - distance(b);
            }
        });

        for(int i = 0; i < n; i++){
            System.out.println(points[i].num);
        }
    }
}
TAGS.

Comments