排序整数列表和字符串Java

时间:2016-06-23 08:34:31

标签: java arrays sorting object

我必须排序包含两个整数和一个字符串的3个对象,我的问题是因为我是java的新手,我如何将对象调用到方法中,这是我的代码到目前为止:那么我和我#39;我想做的是按年龄,名字,一般等级按顺序对所有3个对象进行排序,如何调用所有这些对象以便我可以排序?或者如果我做错了,是什么另类?我需要提前使用List和3个对象。

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);
    }}   

3 个答案:

答案 0 :(得分:0)

您必须使用Comparable或Comparator接口。请参阅此链接,了解如何使用它们。 http://howtodoinjava.com/search-sort/when-to-use-comparable-and-comparator-interfaces-in-java/

研究上面的文章然后尝试,然后在这里提问如果你有任何问题或怀疑。

答案 1 :(得分:0)

我是在本指南的帮助下完成的:http://howtodoinjava.com/search-sort/when-to-use-comparable-and-comparator-interfaces-in-java/

这就是我的主要外观,我创建了另外4个实现比较的类

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 );

    }
}

答案 2 :(得分:0)

一个选项是让您的Student课程成为Comparable<T>实施:

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()方法。