Sort a List

Hi,I have a list which needs to be sorted by 2 list elements say first by stud_num and then by stud_roll. I am trying to do it by using TreeMap but don't know exactly how to take the 2 elements as keys.Your help is greatly appreciated.Thanks,Greesh
[284 byte] By [Greesh09a] at [2007-11-26 15:16:30]
# 1
[url= http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html]The Java?Tutorial - Object Ordering[/url][url= http://www.onjava.com/lpt/a/3286]Making Java Objects Comparable[/url]Let us know if you have questions.~
yawmarka at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for you quick reply but may I know how can I make use of TreeMap to sort the list (list contains Transfer Obejects).Thanks,Greesh
Greesh09a at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 3

Suppose I have data as :

Stud_Num 03, stud_roll 110,name etc

Stud_Num 03, stud_roll 108

Stud_Num 03, stud_roll 112

Stud_Num 03, stud_roll 110

Stud_Num 02, stud_roll 110

Stud_Num 01, stud_roll 110

I need the list to be sorted as

Stud_Num 01, stud_roll 110

Stud_Num 02, stud_roll 110

Stud_Num 03, stud_roll 108

Stud_Num 03, stud_roll 110

Stud_Num 03, stud_roll 110

Stud_Num 03, stud_roll 112

i.e., first by stud_Num then by stud_roll.

Thanks,

Gressh

Greesh09a at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 4

> Thanks for you quick reply but may I know how can I

> make use of TreeMap to sort the list (list contains

> Transfer Obejects).

By reading the tutorial and applying the concepts therein to your code (note that TreeMap will maintain order by the natural ordering of the keys).

[url=http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html]The Java?Tutorial - Object Ordering[/url]

[url=http://www.onjava.com/lpt/a/3286]Making Java Objects Comparable[/url]

If you've tried the techniques from the article(s) and have come up short, let us know what specific difficulty you're having. Otherwise, please review the article(s) and apply the techniques from the article(s) to your code first.

~

yawmarka at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 5

The most simple implementation is to sort the whole list by the first attribute, num, then sort each portion of sequential nums by roll. Meaning once you get them in order by num, sort each group of elements that has the same num value by the roll value. There are probably improvements to be made, which you can discover by googling sorting algroithms.

hunter9000a at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 6
> The most simple implementation......would be to implement Comparable or write a Comparator and let the existing API handle the sorting. No algorithm searches necessary. :o)~
yawmarka at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 7

> > The most simple implementation...

>

> ...would be to implement Comparable or write a

> Comparator and let the existing API handle the

> sorting. No algorithm searches necessary. :o)

>

> ~

Correct me if I'm wrong, but you still have to imlpement the sorting yourself when you use Comparable, right? Meaning Comparable doesn't know automatically how to sort your objects on two fields, it just lets you automatically apply that Comparable to the list.

hunter9000a at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 8

> you still have to imlpement the sorting yourself when

> you use Comparable, right?

Not really. You'll be defining the comparison, not the sorting.

> Meaning Comparable doesn't know

> automatically how to sort your objects on two fields,

> it just lets you automatically apply that Comparable

> to the list.

In order to implement Comparable, you'll need to define the behavior of the compareTo() method, thereby imposing a natural ordering for the class. If it doesn't make sense to have a natural ordering, or if you need more than one method of sorting, implement a Comparator.

~

yawmarka at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 9

Ok, but the comparison is the meat and potatoes that defines the order, and you still have to define that in terms of your attributes. So the comparison would be something like: A comes before B if (A.num < B.num and A.roll < B.roll). It's more compact this way, but the order is still defined by you. The sorting itself is done for you when you apply that comparison.

I think we're both talking about the same thing, and I know I'm splitting hairs, but I'm doing it as much for my own knowledge as for the OP's.

hunter9000a at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 10
I am able to sort the list by stud_num but how to sort it by stud_roll again as sorting by stud_roll would sort the list by only stud_roll and loses the sorting done by stud_Num.Thanks,Greesh
Greesh09a at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 11

> I am able to sort the list by stud_num but how to

> sort it by stud_roll again as sorting by stud_roll

> would sort the list by only stud_roll and loses the

> sorting done by stud_Num.

Pseudocode:

If stud_nums are equal, compare stud_roll.

This is explained and demonstrated in the tutorial. Please let us know if you have any further questions.

~

yawmarka at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 12

> I think we're both talking about the same thing, and

> I know I'm splitting hairs, but I'm doing it as much

> for my own knowledge as for the OP's.

If I read this right, you were going to implement a custom sorting algorithm for this specific problem, something like

sort(List listOfWhateverObjectHasStudNumAndStudRollAsAttributes)

I think the principle of "encapsulate what varies" would be violated there. Why implement a sorting algorithm when it's already been implemented? If all you do is implement the compareTo() method, you're keeping your code slightly more maintainable.

bheilersa at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 13
> If all you do is implement the compareTo() method,> you're keeping your code slightly more maintainable.s/slightly//:o)~
yawmarka at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...
# 14

> > I think we're both talking about the same thing,

> and

> > I know I'm splitting hairs, but I'm doing it as

> much

> > for my own knowledge as for the OP's.

>

> If I read this right, you were going to implement a

> custom sorting algorithm for this specific problem,

> something like

> sort(List

> listOfWhateverObjectHasStudNumAndStudRollAsAttributes)

>

I think the principle of "encapsulate what

> varies" would be violated there. Why implement a

> sorting algorithm when it's already been implemented?

That's not what I was suggesting. Well, I guess I kinda was in my first reply, but not after that, maybe that's where the confusion came from.

> If all you do is implement the compareTo() method,

> you're keeping your code slightly more maintainable.

That's what I was talking about when I was replying to yawmark.

hunter9000a at 2007-7-8 10:53:09 > top of Java-index,Java Essentials,Java Programming...