简体   繁体   中英

Sorting in arraylist

I am trying to sort the elements present in my arraylist in decreasing order. However, there seems to be some issue in the implementation. I am just a beginner in java, and am trying to use the most easiest method possible to sort.

student temp = new student(user_name,given_name,family_name,tot_marks);

for(int j=0;j<list1.size()-1;j++)

{

    for(int k=0;k<list1.size();k++)

    {

        student sort1 = list1.get(j);

        student sort2 = list1.get(k);

        if(sort1.tot_marks < sort2.tot_marks)

        {

            temp.user_name=sort1.user_name;

            temp.family_name=sort1.family_name;

            temp.given_name=sort1.given_name;

            temp.tot_marks=sort1.tot_marks;

            sort2.user_name=temp.user_name;

            sort2.family_name=temp.family_name;

            sort2.given_name=temp.given_name;

            sort2.tot_marks=temp.tot_marks;

            sort1.family_name=sort2.family_name;

            sort1.given_name=sort2.given_name;

            sort1.tot_marks=sort2.tot_marks;

            list1.add(sort1); //Adding sorted elements to the arraylist.

        }

        //If marks are same, sort on the basis of username.

        else if(sort1.tot_marks == sort2.tot_marks)

        {

            //Compare usernames whichever is greater.

            {

                temp.user_name=sort1.user_name;

                temp.family_name=sort1.family_name;

                temp.given_name=sort1.given_name;

                temp.tot_marks=sort1.tot_marks;

                sort2.user_name=temp.user_name;

                sort2.family_name=temp.family_name;

                sort2.given_name=temp.given_name;

                sort2.tot_marks=temp.tot_marks;

                sort1.family_name=sort2.family_name;

                sort1.given_name=sort2.given_name;

                sort1.given_name=sort2.given_name;

                sort1.tot_marks=sort2.tot_marks;

                list1.add(sort1);

            }

        }

    }

}

//Print the sorted list.

for (int i=0;i<list1.size();i++)

{

    student display = list1.get(i);

    System.out.println(display.tot_marks+","+display.given_name+" 
"+display.family_name);

}

You could just do:

Collections.sort(list1, new Comparator<student>() {

    @Override
    public int compare(student one, student another) {
        if (one.tot_marks == another.tot_marks) {
            return 0;
        }
        return one.tot_marks > another.tot_marks ? 1 : -1;
    }
});

However a few advices:

  • Class names like student are not very easy for the eye for java developers. Try using capitalized camel case class names (in this case Student )
  • Underscores in member names are also not very nice in java, try camel case names ( totMarks ). Even better is a geter, and setter for it, rather than leaving it public. ( getTotMarks() , setTotMarks(int) )
  • Also before doing something like you did, try researching a bit! There is a good chance, somebody else wrote it before you!

No need for that. You are adding duplicated to your list.

list1.add(sort1); //Adding sorted elements to the arraylist.

Check your swap logic. It should be:

TEMP = SORT1
SORT1 = SORT2
SORT2 = TEMP

The way to go is to ditch your approach and make your student class implement the Comparable interface: public class student implements Comparable<student> . Also, in Java, class names should start with upper case letters.

Once that you make your class implement this interface, you will be forced to implement the compareTo(student student) method.

In this method, you will implement your comparison logic:

public int compareTo(student student)
{
    if(this.marks != student.marks)
    {
          return Integer.compare(this.marks, student.marks);
    }
    else
    {
          return this.name.compareTo(student.name);
    }
}

Then, to sort your array, simply call like so:

List<student> students = ...
Collections.sort(students);

The above will call your implementation of the .compareTo and sort the array accordingly.

要按降序对arrayList进行排序,请实现Comparator接口,正如Denis正确指出的那样。不要重复元素

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

public class MyArrayListSort {

public static void main(String a[]){

    List<Student> list = new ArrayList<Student>();
    list.add(new Student("Ram",3000));
    list.add(new Student("John",6000));
    list.add(new Student("Crish",2000));
    list.add(new Student("Tom",2400));
    Collections.sort(list,new MyMarkComp());
    System.out.println("Sorted list entries: ");
    for(Student e:list){
        System.out.println(e);
    }
}

}

class MyMarkComp implements Comparator{

@Override
public int compare(Student e1, Student e2) {
    if(e1.getMarks() < e2.getMarks()){
        return 1;
    } else {
        return -1;
    }
}

}

class Student{

private String name;
private int mark;

public Student(String n, int s){
    this.name = n;
    this.salary = s;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getMarks() {
    return mark;
}
public void setMarks(int mark) {
    this.mark = mark;
}
public String toString(){
    return "Name: "+this.name+"-- Marks: "+this.mark;
}

}

In java to sort a List you should use Collections.sort and a Comparator .

Collections.sort(list1, new Comparator<student>() {
    public int compare(student a, student b) {
        if(a.tot_marks < b.tot_marks)
           return -1;
        else if (a.tot_marks > b.tot_marks)
            return 1;
        else
            return a.username.compareTo(b.username);
    }
});

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