7 Replies - 2820 Views - Last Post: 24 October 2011 - 10:26 PM Rate Topic: -----

#1 AuroraMirai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Algorithm to sort array of classes in ascending order

Posted 24 October 2011 - 09:59 PM

Hi guys,
how can one implement an ascending sorting algorithm in array classes?

So Far I read few methods of sorting array but how about array of classes?

Any help is appreciated :)
Is This A Good Question/Topic? 0
  • +

Replies To: Algorithm to sort array of classes in ascending order

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Algorithm to sort array of classes in ascending order

Posted 24 October 2011 - 10:07 PM

Are you familiar with the Comparable interface? If not, you will want your class to implement the Comparable interface.

Now, the Comparable interface has one method for you to implement- compareTo(). This method returns an int based on whether or not the parameter is less than, equals(), or greater than the current object. So when invoking a.compareTo©, a negative, 0, or positive number is returned if a < c, a.equals© or a > c, respectively. You can use invoke the compareTo() method on your Objects to determine their ordering rather than using the <, ==, or > operators.
Was This Post Helpful? 2
  • +
  • -

#3 AuroraMirai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: Algorithm to sort array of classes in ascending order

Posted 24 October 2011 - 10:11 PM

Hi mac thx for the reply,

Is comparable interface an inbuilt class? Sry I am very new to programming and not familiar with the names.
Can you show me a formula to compare 1 object with another? Because I am still not sure what an object is.

Right now I have an array of classes called

Student stu[];

Which contains, name, student ID, assignment mark ect.
How can I sort the class stu[] based on the student ID in ascending order?
If I need to compare the objects how should I write it? I really have no idea :S

Thank for the reply anyway
Was This Post Helpful? 0
  • +
  • -

#4 novacrazy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 117
  • Joined: 01-March 11

Re: Algorithm to sort array of classes in ascending order

Posted 24 October 2011 - 10:12 PM

When sorting arrays, the end result is in ascending order or descending order by some value or key.

3, 2, 6, 4
would turn into:
2, 3, 4, 6
if sorted in ascending order. However, all sorting algorithms need something to compare in order to organize it.
In simple arrays, it compares the value directly, and it can tell that the number 2 needs to go somewhere before 4, and so on. Pretty simple.

Classes though, don't really have anything to sort. However, you can design or alternatively implement, many sorting algorithms to sort the classes based on values within the classes. So instead of a sorting algorithm trying to decide whether class t1 or t2 should come first, when they have no real value, it would test t1.value vs. t2.value, and so on, until it sorts them that way. That'd be the easiest method.

Also, most sorting algorithms rely on swapping values. Swapping two class instances is very... time consuming and difficult, since it would have to go through copy constructors and everything else.

An easier way it to perform the sort on an array of pointers to class instances, and then swap the pointers to the classes, instead of the class instances themselves. Granted, you'd have to make sure they are all pointing to valid, allocated instances before accessing any internal values, or some safeguard for that.

If you'd like an actual coded example, I can give one.

EDIT: Crap, this is Java, so I guess there wouldn't be a way to do that pointer thing. I'm sorry. I wasn't paying attention.

This post has been edited by novacrazy: 24 October 2011 - 10:16 PM

Was This Post Helpful? 2
  • +
  • -

#5 AuroraMirai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: Algorithm to sort array of classes in ascending order

Posted 24 October 2011 - 10:17 PM

Can you please show something for reference? right now I dont even know what should goes where.
I would like to try before asking help but I was just stuck for hours with this question :(.

What I was trying to do is making a temporary class to swap 2 classes but since it was said to be difficult I don't
think its possible 0o. Can you please show an example of your sorting method?

Thank you very much ppl
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Algorithm to sort array of classes in ascending order

Posted 24 October 2011 - 10:20 PM

Yes- Comparable is located in the java.util package. It is built-in.

So I threw together a quick class that implements the Comparable interface. Like ArrayLists, Comparable is generic. So the way I have it set up, A objects are Comparable to each other. Notice in the compareTo() method how I am comparing them based on their values for x. So if this > other, then this.x > other.x, and compareTo() will return a positive number.
class A implements Comparable<A>{
    int x;

    A(int x){ this.x = x; }

    public int getX(){ return x; } 
    
    public int compareTo(A other){ return this.x - other.x; }

}



Now if I have the following snippet, you can run it and see how the comparisons play out. You can see which objects are greater or less than the others, based on their values of x.
A[] objects = new A[3];
Random rand = new Random();

for(int i = 0; i < objects.length; i++){
     objects[i] = new A(rand.nextInt());    
}

for(int i = 0; i < objects.length; i++){
    for(int j = i+1; j < objects.length; j++){
          System.out.println("Value: " + i + "( " + objects[i].getX() + " ), Value " + j + "( " + objects[j].getX() + "): " + objects[i].compareTo(objects[j]));
    }
   
}



Now take this, and apply this as to how you would determine if one Student is greater than another.
Was This Post Helpful? 1
  • +
  • -

#7 AuroraMirai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 21-October 11

Re: Algorithm to sort array of classes in ascending order

Posted 24 October 2011 - 10:25 PM

Thank you very much I will try it now

Sry for double post (Can't find the edit button :P)

to top it up the formula was intended to compare right? How can I actually swap the index of the array.

Like for example is in stu[0] I have MR John with ID 4
and in stu[1] I have Mr Ary Smith with ID 1

How can I swap all the values from index 1 to index 0?

Sry for asking again
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Algorithm to sort array of classes in ascending order

Posted 24 October 2011 - 10:26 PM

You will need a temporary variable. A basic swap algorithm looks like:
temp <-- one
one <-- two
two <-- temp


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1