If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Sort java objects in an Array
Sorting in Java is Easy!
Author: vglass@jfind.com, Van Glass
There once was a time that sorting an array of primitives in Java took some work. And sorting an array of Objects involved even more. Nowadays we can sort arrays of primitives and Objects with relatively little code using the Comparable interface.
The java.util.Comparable interface is a class which when implemented will define the natural order for a pair of Objects. Currently there are only a handful of classes which by default implement the Comparable interface. One such class is the String class. In implementing the Comparable interface the String class overloads the compareTo(Object obj) method. The compareTo(Object obj) method returns a negative integer, zero, or a positive integer where the current instance is less than, equal to, or greater than object received.
The work has already been done for us in the String class. However if you want to provide sorting capabilities for your own classes then you must implement Comparable and overload the compareTo(Object obj) yourself. Here is a simple example which defines a class Student. Each Student has a studentid, first_name, last_name. According to our specifications Student should be sorted by student_id.
import java.util.*;
public class Student implements Comparable
{
public int student_id;
public String last_name;
public String first_name;
public Student(int student_id, String last_name, String first_name)
{
this.student_id = student_id;
this.last_name = last_name;
this.first_name = first_name;
}
/* Overload compareTo method */
public int compareTo(Object obj)
{
Student tmp = (Student)obj;
if(this.student_id < tmp.student_id)
{
/* instance lt received */
return -1;
}
else if(this.student_id > tmp.student_id)
{
/* instance gt received */
return 1;
}
/* instance == received */
return 0;
}
}
Now that the Student class overloads the compareTo(Object obj) method we can easily sort an array of Student objects. This is done using the java.util.Array class and its static sort method. A main method has been added below to demonstrate how this would work.
public static void main(String[] args)
{
/* Create an array of Student object */
Student[] students = new Student[3];
students[0] = new Student(52645,"Smith","Bob");
students[1] = new Student(98765,"Jones","Will");
students[2] = new Student(1354,"Johnson","Matt");
/* Sort array */
Arrays.sort(students);
/* Print out sorted values */
for(int i = 0; i < students.length; i++)
{
System.out.println(students[i].student_id +
students[i].last_name + students[i].first_name);
}
}
Simple right? With a little more work the Student class could be modified to sort on a combination of member variables. For instance you may want to sort first by last_name and then by student_id. Keep in mind that the Arrays class will sort any array including an array of primitives. With primitives however there is no need to implement Comparable making it very easy to sort int, float etc. Until next week.

Student[] students = new Student[3];
students[0] = new Student(new Long (52645),"Smith","Bob",true);
students[1] = new Student(new Long (98765),"Jones","Will",true);
students[2] = new Student(new Long (1354),"Johnson","Matt",true);

You guys are thinking too hard :P
There are tons of ways to do this, from creating you're own sorts, you're own collections, and so forth. But the easiest ways are already built in.
You need to implement a Comparable interface (I'm using generics here, but you can do this without and using 1.4), and make use of the Collections object:
Please note that my main is fluffed since I'm too lazy to create 15 employees by hand :P

This kinda what you're looking for? Works great from you're main, all you need to do is ensure that compareTo exists through Comparable, and that you use Arrays.sort(yourstack, Collections.reverseOrder()). Simple as that

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

while craeting the student object in rn=untime sorting order need to be passed. once it order is passed then it will get sorted what ever the way it wants. This is called dynamic variable sir

did you try this?

Code:

Student[] students = new Student[3];
students[0] = new Student(new Long (52645),"Smith","Bob",true);
students[1] = new Student(new Long (98765),"Jones","Will",false); // looks like Jones does not like being sorted in ascending :O
students[2] = new Student(new Long (1354),"Johnson","Matt",true);
/* Sort array */
Arrays.sort(students);

sorting as an operation should not have any dependency on the operands...Fou-lu has given the solution using the Comparable interface...Here's how you can do it using the Comparator interface (casting aside doubts whether rajeshnaidu has completed his homework :/ )

Yep, IMO the comparator is the best solution.
The comparable works great if its always the same field that you plan on comparing. On the otherhand, you can do the comparator up as an abstract and extend it out to other properties; with my example you could do up an AbsEmpSorter and extend it up for EmpSorterID, EmpSorterFirstName and EmpSorterLastName. Since these are all typeof Comparator it works perfectly inside of the Arrays.sort method.
Another option is to create a collection for the Employee, either by extending a collection or simply holding an array of given values. I would extend the collection, I can't recall offhand if you need to override any of the sort or reverse functions, but I still like containing everything in objects instead of arrays. Combine the collections with comparable objects for it, and you've got one awesome sortable object, with an rsort built in!

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)