Jump to content

Array sorting by based on different criteria

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
heidi7

heidi7

    Newbie

  • Members
  • Pip
  • 8 posts
Hello,
I am a newbie to Java. I am trying to solve the following problem and been stuck from past 3-4 days. I feel I am missing something very basic and hence not able to get the answer.

Problem: Create an array of 4 Student objects. The fields are student_id, student_name,Marks. Sort the students based on student_id and based on Marks. The Arraysort method can be have this signature
void Arraysort(Student[] student_list, String sortcriteria);

Have a method int compareTo(Student Stu, String sortcrtiteria);

Can anybody explain me the way to approach the problem? I woudl like to know the body of methods Arraysort and compareTo. Should I implement and interface? ( I don't have much idead about this).
Appreciate your help.
Thanks.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It will depend on what Student looks like.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
heidi7

heidi7

    Newbie

  • Members
  • Pip
  • 8 posts
oh! Isn't this the only way to write Student array? Are there different ways?

Student[] students ={ new Students(1, Amy, 23), new Students(2, Julian, 20)};

Hope you can help me with this.
Thanks.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
My point was that we don't know what the Student class is (it could have a variety of information in it), which makes it harder to know how you might compare Student's for sorting purposes. Age, First Name, Last Name, GPA, class load, ID, etc. There are MANY sort criteria, depending on what you record. Event with Marks and ID, they could be numeric, strings, other.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
heidi7

heidi7

    Newbie

  • Members
  • Pip
  • 8 posts
My Student Class
import java.util.Arrays;

public class Student
{
public static final String MARKS ="bymarks";
public static final String ID = "byid";

public int student_id;
public int marks;
public String last_name;
public String first_name;

public Student(int student_id, int marks, String last_name, String first_name)
{
this.student_id = student_id;
this.marks= marks;
this.last_name = last_name;
this.first_name = first_name;
}

#6
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
you will need to work with a comparator: click

#7
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
You can make the student object implement Comparable then right it something like this:

int compareTo(student s) {
          if (s.height > height) return 1;
          if (s.height < height) return -1;

          if (s.weight > weight) return 1;
          if (s.weight < weight) return 1;

         return 0;
}

The first if statements test for the most appropriate criteria, and you move down to the lowest level of importance.

#8
heidi7

heidi7

    Newbie

  • Members
  • Pip
  • 8 posts
Thanks for the link. I will read through and give it one more try. :)

Thanks. I will give it try to solve it again.

Edited by WingedPanther, 09 October 2009 - 07:42 AM.
Double post