2016-06-23 2 views
0

2 개의 정수와 1 개의 문자열을 포함하는 3 개의 객체를 정렬해야합니다. 내 질문은 Java에 익숙하지 않았기 때문에 어떻게 객체에 메소드를 호출 할 수 있습니까? 이것이 내 코드입니다. 멀리 : 그래서 내가하려는 것은 나이, 이름, 일반 급료에 따라 모든 3 개의 객체를 정렬하는 것입니다. 어떻게 정렬 할 수 있도록 모든 객체를 호출 할 수 있습니까? 아니면 내가 잘못하고있는 경우, 대체 뭐야? 나는 List와 3 개의 객체를 사용할 필요가있다.정수와 문자열의 정렬 목록 Java

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 

public class Student { 

    int age; 
    int generalGrade; 
    String firstName; 
    String lastName; 

    public Student(int age, String firstName, String lastName, int generalGrade) { 
     this.age = age; 
     this.generalGrade = generalGrade; 
     this.firstName = firstName; 
     this.lastName = lastName; 
    } 

    public static void main (String[] args) { 

     Student myStudent = new Student(19, "A", "X", 100); 
     Student myStudent1 = new Student(20, "B", "P", 98); 
     Student myStudent2 = new Student(19, "C", "N", 70); 

     Age(myStudent); 


    } 

    public void Age(int[] age) { 

     List studentAge = new ArrayList(Arrays.asList(age)); 
     System.out.println("List values before sort " + studentAge); 
     Collections.sort(studentAge); 
     System.out.println("List value after sort: " + studentAge); 
    }} 
+0

사용자 지정 비교기를 작성한 다음 해당 비교기를 사용하여 Collections.sort를 작성하십시오. – Sanjeev

답변

0

나는이 가이드의 도움으로 이것을 수행했다 : http://howtodoinjava.com/search-sort/when-to-use-comparable-and-comparator-interfaces-in-java/.

이 어떻게 내 주요 외모처럼, 나는있는 옵션이 Student 클래스 A Comparable<T> 구현하는 것입니다

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class Main { 
    public static void main(String[] args) { 


     Student student1 = new Student(19, "A", "X", 100); 
     Student student2 = new Student(20, "B", "P", 98); 
     Student student3 = new Student(19, "C", "N", 70); 

     List<Student> students = new ArrayList<Student>(); 
     students.add(student1); 
     students.add(student2); 
     students.add(student3); 

     Collections.sort(students); 
     System.out.println("Sorted students " + students); 

     System.out.println(" "); 
     Collections.sort(students, new NameSort()); 
     System.out.println("Sort by name " + students); 

     System.out.println(" "); 
     Collections.sort(students, new AgeSort()); 
     System.out.println("Sort by age " + students); 

     System.out.println(" "); 
     Collections.sort(students, new SortGrade()); 
     System.out.println("Sort by General Grade " + students); 
     System.out.println(""); 
     System.out.println("Decreasing order"); 
     System.out.println(""); 

     Collections.sort(students, Collections.reverseOrder(new NameSort())); 
     System.out.println("Sort by name " + students); 
     System.out.println(""); 
     Collections.sort(students, Collections.reverseOrder(new AgeSort())); 
     System.out.println("Sort by age " + students); 
     System.out.println(""); 
     Collections.sort(students, Collections.reverseOrder(new SortGrade())); 
     System.out.println("Sort by General grade " + students); 

    } 
} 
0

비교 구현 4 개 다른 클래스 만들었습니다

public class Student implements Comparable<Student> { 

    // your code... 

    @Override 
    public int compareTo(Student student){ 
     // your sorting logic... 
    } 

} 

이 방법을 Collections.sort()은 논리를 사용하여 목록을 정렬합니다.

클래스가 Comparable<T>을 구현할 수없는 경우 사용자 정의 Comparator<T>을 구현하고 Collections.sort() 메서드의이 버전을 사용할 수 있습니다.