Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20210211] Comparator #38

Open
JuHyun419 opened this issue Feb 11, 2021 · 0 comments
Open

[20210211] Comparator #38

JuHyun419 opened this issue Feb 11, 2021 · 0 comments
Labels

Comments

@JuHyun419
Copy link
Owner

Java - Comparator(정렬)

  • compareTo(String s1, String s2)
    • 앞에 값(s1)과 뒤의 값(s2)을 비교해서 리턴값을 양수로 주면 값을 바꿈 -> 오름차순
    • 앞에 값(s1)과 뒤의 값(s2)을 비교해서 리턴값을 음수로 주면 값을 바꾸지 않음 -> 내림차순
    public static String[] solution(String[] strings, int n) {
        Arrays.sort(strings, (s1, s2) -> {
            if (s1.charAt(n) > s2.charAt(n))
                return 1;
            else if (s1.charAt(n) == s2.charAt(n))
                return s1.compareTo(s2);
            return -1;
        });
        return strings;
    }

@JuHyun419 JuHyun419 added the Java label Feb 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant