简体   繁体   中英

How to sort objects of an array in ascending order?

I need to sort the marks in ascending order. It shows me the error: "Incompatible types".

static void sortimi(Studenti[] std ){

    int perk=0;    

    for (int i=0; i<std.length; i++) {
             for(int j=1; j<std.length - i; j++) {
                 if(std[j-1] > std[j]){ // Error: Bad operand types for binary operator
                 perk=std[j-1];          // Error: Incompatible types
                 std[j]=perk;             // Error: Incompatible types
                 }
    }
}

public class Studentat {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        // TODO code application logic here
        Studenti[] std= new Studenti[3];

        for(int i=0; i<std.length;i++){
        std[i]= new Studenti();    
        }

        for(int i = 0; i < std.length; i++){
        System.out.println(std.toString());

         }
    }



public class Studenti {

    private String name;
    private int mark;

    public Studenti(){
    Scanner s = new Scanner(System.in);

    System.out.print("Write student's name:" + " ");
    this.name = s.nextLine();

    System.out.print("Write students' mark:" + " ");
    this.mark = s.nextInt();

    if(mark<5 || mark>10){
                mark = 0;
                System.out.println("The given mark is wrong");        
    }
}

static void sortimi(Studenti[] std ){

    int perk=0;    

    for (int i=0; i<std.length; i++) {
             for(int j=1; j<std.length - i; j++) {
                 if(std[j-1] > std[j]){ // Error: Bad operand types for binary operator
                 perk=std[j-1];          // Error: Incompatible types
                 std[j]=perk;             // Error: Incompatible types
                 }
    }
}
    }

First, in sortimi(Studenti[] std) , just declare perk to be of type Studenti instead of int :

Studenti perk = null;

Also, you seem to have declared the method sortimi(Studenti[] std) twice. You'll have to get rid of one of them.

Finally, you can't compare instances of Studenti using the > operator. You'll have to define some sort of comparison method or else sort on a field. For instance:

public class Studenti {
    ...
    public boolean precedes(Studenti other) {
        // logic for deciding order for students
    }
}

Alternatively (and preferably, in my opinion), you can declare one or more static nested classes of Studenti that implements the Comparator<Studenti> interface . Then to sort the array you can just call Arrays.sort() and pass an instance of the appropriate comparator class. The advantage of this approach is that you can have several classes that sort on different criteria (eg, student name only vs. by mark and then by student name). The reason I recommend static nested class is that your Studenti class provides no mechanism for outside classes to access the field values. If you made the fields final public instead of private or if you provided accessor methods, you could make the comparator classes separate, top-level classes and keep the Studenti class fairly streamlined.

To sort an item the item should be comparable. Here you can do it two ways. So we need Student class to be Comparable

public class Studenti extends Comparable<Studenti> {
private String name;
private int mark;

public Studenti() {
    Scanner s = new Scanner(System.in);

    System.out.print("Write student's name:" + " ");
    this.name = s.nextLine();

    System.out.print("Write students' mark:" + " ");
    this.mark = s.nextInt();

    if (mark < 5 || mark > 10) {
        mark = 0;
        System.out.println("The given mark is wrong");
    }

}

public int getMark() {
    return mark;
}

public void setMark(int newMark) {
    this.mark = newMark;
}

@Override
public int compareTo(Studenti o) {
    return o.getMark()  - this.mark; //Change this statement for reversing the sort order.
}

}

now you can do

Arrays.sort(studentsArray); Refer this for more details https://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM