HR자바 Java Sort

개요[ | ]

HR자바 Java Sort
해커랭크 Java
# 문제 비고
HR자바 Data Structures e
28 HR자바 Java 1D Array
29 HR자바 Java 2D Array
30 HR자바 Java Subarray
31 HR자바 Java Arraylist
32 HR자바 Java 1D Array (Part 2)
33 HR자바 Java List
34 HR자바 Java Map
35 HR자바 Java Stack
36 HR자바 Java Hashset
37 HR자바 Java Generics
38 HR자바 Java Comparator
39 HR자바 Java Sort
40 HR자바 Java Dequeue
41 HR자바 Java BitSet
42 HR자바 Java Priority Queue

import java.util.*;

class Student{
	private int id;
	private String fname;
	private double cgpa;
	public Student(int id, String fname, double cgpa) {
		super();
		this.id = id;
		this.fname = fname;
		this.cgpa = cgpa;
	}
	public int getId() {
		return id;
	}
	public String getFname() {
		return fname;
	}
	public double getCgpa() {
		return cgpa;
	}
}

//Complete the code
public class Solution
{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int testCases = Integer.parseInt(in.nextLine());
		
		List<Student> studentList = new ArrayList<Student>();
		while(testCases>0){
			int id = in.nextInt();
			String fname = in.next();
			double cgpa = in.nextDouble();
			
			Student st = new Student(id, fname, cgpa);
			studentList.add(st);
			
			testCases--;
		}
        
        studentList.sort(new Comparator<Student>() {
            public int compare(Student s1, Student s2)  {
                int c;
                c = Double.valueOf(s1.getCgpa()).compareTo(s2.getCgpa());
                if( c != 0 ) return -c;
                c = s1.getFname().compareTo(s2.getFname());
                if( c != 0 ) return c;
                return Integer.valueOf(s1.getId()).compareTo(s2.getId());
           }
        });
      
      	for(Student st: studentList){
			System.out.println(st.getFname());
		}
	}
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}