0

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);
}
asked Apr 21, 2014 at 8:51
3
  • 2
    You do not need to manually sort your array, check this question: stackoverflow.com/questions/2784514/… Commented Apr 21, 2014 at 8:54
  • You could implement a comparator and use the built-in sort methods. And if you're interested you should look up some sorting algorithms, like Quicksort, Bubblesort, Insertionsort etc. Commented Apr 21, 2014 at 8:54
  • it shows poor design. Commented Apr 21, 2014 at 8:54

6 Answers 6

1

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!
answered Apr 21, 2014 at 9:03

Comments

0

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);
 }
});
answered Apr 21, 2014 at 9:02

Comments

0

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
answered Apr 21, 2014 at 8:58

2 Comments

I am trying to swap all the values which Sort1 and Sort2 objects are pointing to. Implementing above 3 LOCs will only swap the references of the object, isnt it?
It's a pseudo-code only. Convert it to Java code your way.
0

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.

answered Apr 21, 2014 at 9:00

3 Comments

this link is helpful : mkyong.com/java/…
I am trying not to use collections. I am trying to sort through a manual approach.
@MrCoder: Then you might consider Denis Kulagin's approach. If this is homework it is recommended that you tag your question as so. You also do not need to copy each element. Assigning your array elements to temporary variables will work (in this case).
0

To Sort the arrayList in descending order please implement the Comparator interface and as Denis rightly pointed out.Do not duplicate the elements

answered Apr 21, 2014 at 9:01

Comments

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

}

answered Apr 21, 2014 at 9:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.